我正在尝试使用以下代码来获取android键盘的高度。

popupWindow解决方案在某些设备中不起作用,还有其他解决方案吗?

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

                }
            });

最佳答案

我将使用insets解决方案-其中,rootWindowInsets.systemWindowInsetBottom将包括键盘(如果存在)。看看documentation

10-07 13:54