获取 Android 键盘高度

3

我正在尝试使用以下代码获取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

我使用这种方法来计算键盘的高度:

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;

}


它总是返回键盘高度为0! - andi devol
我将活动的父布局设置为viewMain。 - andi devol

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

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

我不想隐藏键盘,我想获取键盘的尺寸。 - andi devol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接