【问题标题】:Android : getting soft keyboard heightAndroid:获取软键盘高度
【发布时间】:2021-07-14 23:29:09
【问题描述】:

作为标题,我只想得到键盘高度。

由于Android手机分辨率太多,难以驾驭。

那么有什么 API 可以做到吗?

谢谢大家。

【问题讨论】:

    标签: android keyboard


    【解决方案1】:

    在您的活动中使用此代码:

     iSoftkeyboardHeight = viewMain.getHeight(); // viewMain - root view of your activity
    
     int y = iSoftkeyboardHeight - 2;
     int x = 10;
     int counter = 0;
     int height = y;
     int iSoftkeyboardHeightNow = 0;
     Instrumentation instrumentation = new Instrumentation();
     while( true)
      {
        final MotionEvent m = MotionEvent.obtain(  SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent m1 = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,   x, y, 0);
        boolean ePointerOnSoftkeyboard = false;
    
        try
          {
            instrumentation.sendPointerSync(m);
            instrumentation.sendPointerSync(m1);
          }
         catch (SecurityException e)
          {
             ePointerOnSoftkeyboard = true;
          }
        if( !ePointerOnSoftkeyboard)
          {
            if( y == height)
              {
                if( counter++ < 100)
                  {
                    Thread.yield();
                    continue;
                  }
              }
             else
              if( y > 0)
                iSoftkeyboardHeightNow = iSoftkeyboardHeight - y;
            break;
          }
        y--;
        m.recycle();
        m1.recycle();
      }
     if( iSoftkeyboardHeightNow > 0)  iSoftkeyboardHeight = iSoftkeyboardHeightNow;
      else iSoftkeyboardHeight = 0;
    
     // now use iSoftkeyboardHeight
    

    【讨论】:

    • 我在显示键盘后尝试了这段代码,但它使应用程序崩溃。
    猜你喜欢
    • 2013-08-28
    • 2015-05-06
    • 2014-10-06
    • 1970-01-01
    • 2014-05-26
    • 2015-03-18
    • 2012-12-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多