參考:https://blog.csdn.net/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;
}
}