监听软键盘的弹出隐藏

原理在于软键盘弹出后,rootView 的高度会变小。

int rootViewLastHeight;
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (rootViewLastHeight == 0) {
                    rootViewLastHeight = rootView.getHeight();
                } else {
                    if (rootView.getHeight() > rootViewLastHeight) {
                        // hide
                    } else {
                        // show
                    }
                    rootViewLastHeight = rootView.getHeight();
                }
            }
        });

猜你喜欢

转载自blog.csdn.net/gdeer/article/details/53546714