自己管理键盘状态
先定义一个接口
public interface OnKeyboardListener {
void isShow(boolean isShow);
}
public OnKeyboardListener getOnKeyboardListener() {
return onKeyboardListener;
}
public void setOnKeyboardListener(OnKeyboardListener onKeyboardListener) {
this.onKeyboardListener = onKeyboardListener;
}
//键盘状态监听
private OnKeyboardListeneronKeyboardListener;
在Activity的跟布局layout上做监听
main_layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r =new Rect();
main_layout.getWindowVisibleDisplayFrame(r);
if (main_layout.getRootView().getHeight() - (r.bottom - r.top) > CodeUtil.dip2px(WDYBaseActivity.this,100)//键盘高度) {
setIsShowKeyboard(true);
if (getOnKeyboardListener() !=null)
getOnKeyboardListener().isShow(true);
}else {
setIsShowKeyboard(false);
if (getOnKeyboardListener() !=null)
getOnKeyboardListener().isShow(false);
}
}
});
原理:监听根布局是否被顶高了。用于解决键盘管理混乱的页面。
网友评论