Android:获得软键盘高度

时间:2014-07-15 10:16:41

标签: android keyboard

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

由于Android手机上有很多分辨率,因此很难处理。

那么有没有API?

谢谢大家。

1 个答案:

答案 0 :(得分:0)

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

 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