您的位置:首页 > 移动开发 > Android开发

android监听软键盘隐藏

2017-09-08 17:42 281 查看
从网上看到的是根据根布局的高度变化来监听软键盘的显示和隐藏,但是如果设置了adjustpan即根布局高度始终不变的时候是无效的,搜了好长时间才找到解决方法。

//获取到根布局,这个根布局和setContenView的布局不一样。
rootView = getWindow().getDecorView().findViewById(android.R.id.content);
//然后设置布局的监听
rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
//让本类继承接口实现方法
@Override
public void onGlobalLayout() {
if(isKeyboardShown(rootView)){
}else {
//          Log.e(TAG, "onGlobalLayout: " );
//          mImmersionBar.fixMarginAtBottom(true).init();
}
}
//这个是最主要的方法,根据根布局可视的高度来判断软键盘的显示和隐藏
private boolean isKeyboardShown(View rootView) {
final int softKeyboardHeight = 100;
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
int heightDiff = rootView.getBottom() - r.bottom;
return heightDiff > softKeyboardHeight * dm.density;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: