监听软键盘显示或隐藏

监听软键盘显示或隐藏

cl是editText的父布局
cl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                cl.getWindowVisibleDisplayFrame(r);
                int screenHeight = cl.getRootView()
                        .getHeight();
                int heightDifference = screenHeight - (r.bottom);
                CommonUtil.debug("123===", "--高度变化-->>>>>>>"+heightDifference);
                if (heightDifference > 400) {
                    //软键盘显示
                    mIsShoKeyborad = true;
                } else {
                    //软键盘隐藏
                    if (!mIsShoKeyborad) {
                        return;
                    }
                    mIsShoKeyborad = false;
                    if (mUseMaxMet.hasFocus()) {
                        CommonUtil.debug("123===", "---->>>>>>>1");
                        CommonUtil.closeKeyboard(MemberCheckOutActivity.this, mUseMaxMet);
                        //处理逻辑...
                    }
                }
            }
        });

猜你喜欢

转载自blog.csdn.net/wdd1324/article/details/80625076