android开发获取键盘高度以及判断键盘是否显示
public int getKeyboardHeight(Context context){
try {
InputMethodManager im = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
Method method = im.getClass().getDeclaredMethod("getInputMethodWindowVisibleHeight");
method.setAccessible(true);
Object height = method.invoke(im);
return Integer.parseInt(height.toString());
}catch (Throwable e){
return -1;
}
}
View contentView = findViewById(R.id.content_view);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = contentView.getRootView().getHeight() - contentView.getHeight();
if (heightDiff > 0.25 * contentView.getRootView().getHeight()) {
}
}
});