AbrahamCaiJin / CommonUtilLibrary

快速开发工具类收集,史上最全的开发工具类,欢迎Follow、Fork、Star
https://github.com/AbrahamCaiJin/CommonUtilLibrary
2.83k stars 802 forks source link

软键盘管理AppKeyBoardMgr中判断软键盘是否显示的方法是无效的 #27

Open lindroy opened 6 years ago

lindroy commented 6 years ago

目前我试过有效的方法是监听软键盘弹出时引起的根布局高度的变化,当其变化值大于屏幕高度的三分之一时,即视为软键盘弹出。

llRoot.getViewTreeObserver().addOnGlobalLayoutListener(new 
ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                llRoot.getWindowVisibleDisplayFrame(rect);
                int screenHeight = ScreenUtils.getSreenHeight(context);
                int heightDiff = screenHeight - rect.bottom;
                if (heightDiff <= screenHeight / 3) {
                    isKeyboardShown = false;
                } else {
                    isKeyboardShown = true;

                }
            }
        });