Android 最有效的管理软键盘开启、关闭

时间:2022-03-27 02:01:17
protected void showKeyboard(boolean isShow) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

if (null == imm)
return;

if (isShow) {
if (getCurrentFocus() != null) {
//有焦点打开
imm.showSoftInput(getCurrentFocus(), 0);
} else {
//无焦点打开
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
} else {
if (getCurrentFocus() != null) {
//有焦点关闭
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} else {
//无焦点关闭
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}