参考:https://blog.youkuaiyun.com/stwstw0123/article/details/47108337
root为所在布局文件的根布局的对象绑定
root = view.findViewById(R.id.content);
这里我们为了更方便的使用,将做个操作封装成一个方法:
public boolean getKeybordStatus(){
final int[] screenHeight = new int[1];
final int[] myHeight = new int[1];
final int[] heightDiff = new int[1];
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
screenHeight[0] = root.getRootView().getHeight();
myHeight[0] = root.getHeight();
heightDiff[0] = screenHeight[0] - myHeight[0];
Log.e("onGlobalLayout", "screenHeight=" + screenHeight[0]);
Log.e("onGlobalLayout", "myHeight=" + myHeight[0]);
}
});
if (heightDiff[0] > 100) {
Log.e("onGlobalLayout", "Soft keyboard showing"+heightDiff[0]);
return true;
} else {
Log.e("onGlobalLayout", "Soft keyboard hidden"+heightDiff[0]);
return false;
}
}