隐藏软键盘只需要两句话:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏软键盘
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
/** * 隐藏软键盘 */ public void hideImm(){ InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // 隐藏软键盘 imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); }
下面为其他方法
显示软键盘:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(getWindow().getDecorView(),InputMethodManager.SHOW_FORCED);
在Fragment中不能这样获取View的对象 需要getActivity().getWindow().getDecorView()
最好判断一下getActivity是否为空。
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getActivity != null)
imm.showSoftInput(getActivity().getWindow().getDecorView(),InputMethodManager.SHOW_FORCED);
隐藏软键盘:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 隐藏软键盘
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
Fragment中与显示同理。
manifest中默认不弹出软键盘:
android:windowSoftInputMode="adjustUnspecified|stateHidden"