繁体   English   中英

Android:键盘高度变软

[英]Android : getting soft keyboard height

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

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

那么是否有任何API?

谢谢大家

在您的活动中使用以下代码:

 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM