获取Android键盘高度



我正在尝试使用以下代码获得android键盘高度

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    parentLayout.getWindowVisibleDisplayFrame(r);
                    int screenHeight = parentLayout.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom);
                    previousHeightDiffrence = heightDifference;
                    if (heightDifference > 100) {
                        isKeyBoardVisible = true;
                        changeKeyboardHeight(heightDifference);

                    } else {
                        if(emojiKeyboard.getVisibility()==View.INVISIBLE){
                            emojiKeyboard.setVisibility(View.GONE);
                        }
                        isKeyBoardVisible = false;
                    }
                }
            });

这与android:windowSoftInputMode="adjustPan"配合得很好,但当默认键盘出现时,我的活动屏幕会向上移动。我所需要的只是获得键盘高度,并在默认键盘下显示我的自定义键盘,据我说,这只适用于android:windowSoftInputMode="adjustPan"android:windowSoftInputMode="AdjustNothing"。如果设置为android:windowSoftInputMode="adjustNothing",那么我就无法获得键盘高度。我的问题需要另一种解决办法。提前感谢!

我使用此方法计算键盘高度:

public static int getKeyboardHeight() {
 // viewMain <-- findViewById( android.R.id.content).getRootView();
 if( viewMain != null)  iSoftkeyboardHeight = viewMain.getHeight();
 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;
 return iSoftkeyboardHeight;

}

rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect r = new Rect();
        parent.getWindowVisibleDisplayFrame(r);
        int screenHeight = parent.getRootView().getHeight();
        int heightDifference = screenHeight - (r.bottom - r.top);
        Log.d("Keyboard Size", "Size: " + heightDifference);
    }
});

试试这个代码-

public static void hideSoftKeyboard(EditText editText) {
    if (null != editText) {
        InputMethodManager imm = (InputMethodManager) editText.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
}

相关内容

  • 没有找到相关文章

最新更新