我正在使用 DialogFragment, ,虽然我已经成功设置了一个图像以关闭(即解散)对话框时,但是当用户单击其外部任何地方时,我很难找到解开对话框的方法,就像它可以与普通对话框一起使用。我以为会有某种

dialogFragment.setCanceledOnTouchOutside(true);

打电话,但我在文档中看不到这一点。

这可能有可能 DialogFragment 根本吗?还是我在错误的地方看?我尝试拦截“父母”活动中的触摸事件,但除了没有任何触摸事件外,这对我来说似乎并不正确。

有帮助吗?

解决方案

DialogFragment.getDialog().setCanceledOnTouchOutside(true);

必须被调用 onCreateView (正如Apurv Gupta指出的那样)。

其他提示

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       ...
       getDialog().setCanceledOnTouchOutside(true);
       ... 
       }
    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);

        return dialog;
    }

这里有很多答案,但是对话框打开时,应用程序崩溃。写作 getDialog().setCanceledOnTouchOutside(true); 里面 onCreateView 没有工作并撞到了我的应用程序。

(我在用 AppCompatActivity 作为我的抑制性和 android.app.DialogFragment 作为我的碎片)。

有效的是以下两个行:

getDialog()。setCanceLedOntouchoutside(true);

或者

this.getDialog()。setCanceLedOntouchoutside(true);

里面 onActivityCreated

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom;
        //getDialog().getWindow().setDimAmount(0.85f);
        getDialog().setCanceledOnTouchOutside(true);//See here is the code
    }

什么不使用:

dialogFragment.getDialog()。setCanceLedOntouchoutside(false);

抛出以下错误

enter image description here

并在 onCreateView 崩溃了应用!如果您发现错误,请更新答案。

DialogFragment.getDialog().setCanceledOnTouchOutside(false);

这是错误的。我有同样的问题。对于Java和Android Mono的Mono来说,这很好:

this.getDialog().SetCanceledOnTouchOutside(false);

我建议仅在尝试以上解决方案后才使用我的解决方案。我已经描述了我的解决方案 这里. 。简而言之,我正在检查dialogfragment.getView()的触摸界。当触摸点在外面对话范围外时,我正在解散对话框。

            Dialog.SetCanceledOnTouchOutside(true);

为我工作
我的代码

class dlgRegister : DialogFragment
        {
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
    ....
    ....
    }
    public override void OnActivityCreated(Bundle savedInstanceState)
            {
                Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
                Dialog.SetCanceledOnTouchOutside(true);
                base.OnActivityCreated(savedInstanceState);
                Dialog.Window.Attributes.WindowAnimations =    Resource.Style.dialog_animation;
            }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top