您的位置:首页 > 其它

如何监听软键盘的显示与隐藏

2017-04-09 14:18 253 查看
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView()
.getHeight();
int heightDifference = screenHeight - (r.bottom);
if (heightDifference > 200) {
isKeyBoardVisible = true;
// changeKeyboardHeight(heightDifference);
} else {
isKeyBoardVisible = false;
}
}
});


//parentLayout为布局父控件,因为当软件盘显示和隐藏时当windowSoftInputMode为adjustResize时parentLayout会调

//用onsizechange方法,因此对布局树进行监听可以判读出软件盘的显示与隐藏
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: