这属于事件 (KeyEvent),能监听到
软键盘弹起时 Activity 的整体布局会变
判断根布局的可视区域与屏幕底部的差值,如果这个差大于某个值,可以认定键盘弹起了。
参考代码
private void setListenerToRootView() {
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LogUtils.d(TAG, "[onGlobalLayout] .. in ..");
boolean mKeyboardUp = isKeyboardShown(rootView);
if (mKeyboardUp) {
LogUtils.d(TAG, "键盘弹出..");
} else {
LogUtils.d(TAG, "键盘收起..");
}
}
});
}
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;
}
http://blog.csdn.net/xiaole0313/article/details/51537809
http://blog.csdn.net/yanjunhui2011/article/details/52472294