dismiss()方法优先于onDestroy()执行,重写dismiss,获取dialog中有焦点view,隐藏软键盘;当点击dialog外部区域时,监听触摸事件,关闭软键盘:
//重写dismiss,获取dialog中有焦点view,隐藏软键盘
@Override
public void dismiss() {
View view = getDialog().getCurrentFocus();
if(view instanceof TextView){
InputMethodManager mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
super.dismiss();
}
//点击dialog外部区域时,监听触摸事件
private void initSoftInputListener() {
getDialog().getWindow().getDecorView()
.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
InputMethodManager manager = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getDialog().getCurrentFocus() != null
&& getDialog().getCurrentFocus().getWindowToken() != null) {
manager.hideSoftInputFromWindow(
getDialog().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return false;
}
});
}
网友评论