首先EditText所在的父布局或最外层的布局需要添加属性:
android:clickable="true"
android:focusableInTouchMode="true"
public void setHideKeyBoard(final Context context,EditText etView) {
etView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus){
hideKeyboardViewCommon(context,view);
}
}
});
}
public void hideKeyboardViewCommon(Context context,View view) {
InputMethodManager manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));
if (manager != null)
manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
调用:
//点击软键盘外部,收起软键盘
DigitKeyboardUtils digitKeyboardUtils = new DigitKeyboardUtils();
digitKeyboardUtils.setHideKeyBoard(getActivity(),etTransmoney);
// 进入下一个界面,直接调用 hideKeyboardViewCommon即可,因为EditText没有焦点变化时,调用 setHideKeyBoard 方法不会收起键盘
DigitKeyboardUtils digitKeyboardUtils = new DigitKeyboardUtils();
digitKeyboardUtils.hideKeyboardViewCommon(getActivity(),etTransmoney);