【问题标题】:How to get keyboard height when adjust nothing?什么都不调整时如何获得键盘高度?
【发布时间】:2018-10-24 22:03:03
【问题描述】:

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

                }
            });

【问题讨论】:

标签: android keyboard height popupwindow adjustpan


【解决方案1】:

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

【讨论】:

  • 你能给我更多的信息吗?我已经添加了 ViewCompat.setOnApplyWindowInsetsListener(rootView),但是这个回调在创建 Activity 时只调用了一次,并且在我打开/关闭键盘时不做任何事情。
  • 您可以随时拨打View.requestApplyInsets()强制新电话
  • 是的,但是如果我在打开/关闭键盘时没有收到任何回调,我怎么知道我应该何时调用它?
【解决方案2】:
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;
            }
        });

link 的完整代码。此链接对 AdjustNothing 也有帮助。

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多