显示输入法:
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEt, InputMethodManager.SHOW_FORCED);
实际使用时发现没用,但是使用view.postDelayed有效.
隐藏输入法:
很多博文推荐使用下面这个,但是我试了没用
imm.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
有用的是下面这个
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
另外, 如果是在activity中,使用以下代码,亲测有效
public static void hide_keyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if(view == null) {
view = new View(activity);
}
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}