获取Android键盘高度

时间:2015-03-05 07:58:27

标签: android android-layout keyboard android-input-method

我正在尝试使用以下代码获取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",那么我就无法获得键盘高度。 我需要替代解决方案解决我的问题。 提前谢谢!

3 个答案:

答案 0 :(得分:0)

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

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;

}

答案 1 :(得分:0)

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);
    }
});

答案 2 :(得分:-3)

试试这段代码 -

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