Android开发中经常会遇到隐藏键盘的需求,例如登录界面,输入账号密码后需要点击界面其它部位,隐藏键盘。为此,分享前几年自己使用的方法,最近亲自测试,仍然有效。
方法一、Window window = getWindow();
Window window = getWindow();
if (window != null) {
// 关闭键盘
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
方法二、InputMethodManager
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (getCurrentFocus() != null && null != imm) {
Log.d("hideSoftInput", "======hideSoftInput: ==2222222======");
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}