1、在OnCreat()方法中写下面代码
隐藏输入框
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
有可编辑键盘
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
2、有时1的方法可能会失效,需要用的下面的方法
//软键盘隐藏
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
3、当方法1、2不能满足需求的时候
//动态隐藏阮键盘
public static void hideSoftInput(Activity activity){
View view = activity.getWindow().peekDecorView();
if(view != null){
InputMethodManager inputmanager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public static void hideSoftInput(Context context,EditText edit){
edit.clearFocus();
InputMethodManager inputmanager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanager.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
// 动态显示软键盘
public static void showSoftInput(Context context,EditText edit){
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);
}
4、
// 点击空白区域 自动隐藏软键盘
public boolean onTouchEvent(MotionEvent event) {
if (null != this.getCurrentFocus()) {
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
return super.onTouchEvent(event);
}