安卓:越来越软键盘高度 [英] Android : getting soft keyboard height

查看:155
本文介绍了安卓:越来越软键盘高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题,我只是想获得键盘高度。

As the title, I just want to get the keyboard height.

由于有在Android手机上这么多的分辨率,这是很难处理的。

Since there are so many resolution on Android phones, it is hard to handle.

那么,有没有API是什么?

So Is there any API for that?

感谢所有。

推荐答案

在您的活动中使用此code:

Use this code in your Activity:

 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

这篇关于安卓:越来越软键盘高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆