原理在于软键盘弹出后,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();
}
}
});