监测软键盘的弹出和收起

 boolean isVisiableForLast = false;
    int keyboardHeight = 0;
    public void addOnSoftKeyBoardVisibleListener(Activity activity) {
        final View decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                //计算出可见屏幕的高度
                int displayHight = rect.bottom - rect.top;
                //获得屏幕整体的高度
                int hight = decorView.getHeight();
                //获得键盘高度
                keyboardHeight = hight-displayHight;
                boolean visible = (double) displayHight / hight < 0.8;
                if(visible != isVisiableForLast){
//                    listener.onSoftKeyBoardVisible(visible,keyboardHeight );
                    if (visible){//软键盘显示
                        
                    }else{ //软键盘隐藏
                      
                    }
                }
                isVisiableForLast = visible;
            }
        });
    }


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页