public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) {
final View decorView = activity.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHight = rect.bottom - rect.top;
int hight = decorView.getHeight();
boolean visible = (double) displayHight / hight < 0.8;
Log.d(TAG, "DecorView display hight = " + displayHight);
Log.d(TAG, "DecorView hight = " + hight);
Log.d(TAG, "softkeyboard visible = " + visible);
if(visible != sLastVisiable){
listener.onSoftKeyBoardVisible(visible);
}
sLastVisiable = visible;
}
});
}
您可能感兴趣的文章:Android监听软键盘弹出与隐藏的两种方法android监听软键盘的弹出与隐藏的示例代码Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现Android App实现监听软键盘按键的三种方式Android 监听软键盘状态的实例详解Android监听键盘状态获取键盘高度的实现方法