方法一:用于关闭或打开软键盘。都是下面这句方法。
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//关闭软键盘
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
实现点击提交后,关闭EditText的软键盘,用上面方法,需要先判断EditText是否获得焦点。
该方法实现了软键盘的开启和关闭的切换。开始时需将焦点放在EditText控件的父控件上,不然用上述方法会打开软键盘。
将焦点放在父控件上,加下面两句xml
android:focusable="true"
android:focusableInTouchMode="true"
方法二:关闭软键盘。需获取当前view控件传入做参数。
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}