如何隐藏屏幕键盘当DialogFragment由setCanceledOnTouchOutside活动取消 [英] How to hide the onscreen keyboard when a DialogFragment is canceled by the setCanceledOnTouchOutside event

查看:2145
本文介绍了如何隐藏屏幕键盘当DialogFragment由setCanceledOnTouchOutside活动取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个EditText是当前焦点,并且用户的DialogFragment的点击之外;我想在屏幕上的键盘消失。我可以得到它的工作的时候,DialogFragment被驳回是这样的:

If an edittext is currently focused and the user clicks outside of the DialogFragment; I want the on screen keyboard to disappear. I can get it to work for when the DialogFragment is dismissed this way:

InputMethodManager imm;
public View onCreateView(LayoutInflater inflator, ViewGroup container,
        Bundle savedInstanceState) {
imm = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
...}

@Override 
public void dismiss(){
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
    super.dismiss();
}

但是,如果我尝试同样的事情,当它通过触摸dialogfragment以外的取消,将无法正常工作。我试图通过重写OnCancel的像这样做到这一点:

However, if I try the same thing for when it is canceled by touching outside of the dialogfragment, it will not work. I am trying to do this by overriding onCancel like so:

@Override
public void onCancel(DialogInterface dialog){
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
    super.onCancel(dialog);
}

该函数被调用时,外界触摸事件发生,但键盘不会被删除。

The function is called when the outside touch event happens, but the keyboard is not removed.

推荐答案

我有同样的问题,解决它的活动下,把这个在AndroidManifest,我产卵DialogFragment:

I had the same issue and solved it by putting this in the AndroidManifest under the activity where I spawn the DialogFragment:

android:windowSoftInputMode="stateHidden"

这篇关于如何隐藏屏幕键盘当DialogFragment由setCanceledOnTouchOutside活动取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆