文章标题
程序员文章站
2022-04-21 17:57:12
...
Android 监听软键盘的 显示与隐藏
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//获取View可见区域的bottom
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
if (bottom != 0 && oldBottom != 0 && bottom - rect.bottom <= 0) {
Log.e("", "------软键盘隐藏");
} else {
Log.e("", "------软键盘弹出");
}
}
});
注意:监听方法为addOnLayoutChangeListener如果多次调用会出现重复的情况