如何在不进行任何调整的情况下获取键盘高度?

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

                }
            });

请尝试访问以下链接:https://dev59.com/AGYr5IYBdhLWcg3wnrd0。 - Rohan Lodhi
2个回答

1
我会选择使用“插入”解决方案——其中rootWindowInsets.systemWindowInsetBottom将包括键盘(如果存在)。请查看文档

1
你能给我更多的信息吗?我已经添加了ViewCompat.setOnApplyWindowInsetsListener(rootView),但是这个回调只在Activity创建时被调用一次,当我打开/关闭键盘时它不起作用。 - Den
你可以随时调用 View.requestApplyInsets() 来强制进行新的调用。 - Filipkowicz
1
是的,但如果键盘打开/关闭时我没有收到任何回调,我怎么知道该什么时候调用它呢? - Den

-1
final Window mRootWindow = getWindow();
        ll_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            public void onGlobalLayout(){
                int screenHeight = ll_main.getRootView().getHeight();
                Rect r = new Rect();
                View view = mRootWindow.getDecorView();
                view.getWindowVisibleDisplayFrame(r);

                int keyBoardHeight = screenHeight - r.bottom;
            }
        });

完整代码请参考链接。该链接还有助于AdjustNothing。


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