protected void hideInputKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/*
* 这种方法强制显示软键盘时一定要先确保view 获取了焦点,可以调用view.requestFocus()方法
*/
protected void showInputKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
}
protected void autoShowAndHide(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
protected void detectIfIsShowInputKeyboard(View v) {
if (getCurrentSoftInputHeight() > 200){
Toast.makeText(MainActivity.this, "显示", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "没显示", Toast.LENGTH_SHORT).show();
}
}
private int getCurrentSoftInputHeight() {
final View decorView = this.getWindow().getDecorView();
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
// 获取屏幕的高度(包括状态栏,导航栏)
int screenHeight = decorView.getRootView().getHeight();
int keySoftHeight = screenHeight - rect.bottom;
return keySoftHeight;
}
参考: