在对话框外部按下时如何关闭 DialogFragment?

发布于 2024-12-19 23:31:30 字数 303 浏览 2 评论 0原文

我正在使用 DialogFragment ,虽然我已成功设置图像以在按下时关闭(即关闭)对话框,但我很难找到当用户单击任意位置时关闭对话框的方法在它之外,就像它在普通对话框中一样。我以为会有某种调用

dialogFragment.setCanceledOnTouchOutside(true);

,但我在文档中没有看到这一点。

这对于 DialogFragment 是否可行?或者我找错地方了?我尝试拦截“父”活动中的触摸事件,但除了没有收到任何触摸事件之外,这对我来说似乎不正确。

I am using a DialogFragment, and while I have successfully set an image to close (i.e. dismiss) the dialog when pressed, I am having a hard time finding the way to dismiss the dialog when the user clicks anywhere outside it, just as it works with normal dialogs. I thought there would be some sort of

dialogFragment.setCanceledOnTouchOutside(true);

call, but I don't see that in the documentation.

Is this possible with DialogFragment at all? Or am I looking in the wrong places? I tried intercepting touch events in the 'parent' activity but apart from not getting any touch event, it didn't seem right to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

七分※倦醒 2024-12-26 23:31:31

这里有很多答案,但是当对话框打开时应用程序崩溃。
onCreateView 内写入 getDialog().setCanceledOnTouchOutside(true); 不起作用,并使我的应用程序崩溃。

(我使用 AppCompatActivity 作为我的 BaseActivity,使用 android.app.DialogFragment 作为我的 Fragment)。

有效的是以下两行之一:

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);

引发以下错误

在此处输入图像描述

onCreateView 中编写代码会使应用程序崩溃!
如果您发现有问题,请更新答案。

Lot of answers here but, the app crash when dialog opens.
Writing getDialog().setCanceledOnTouchOutside(true); inside onCreateView did not work and crashed my app.

(I am using AppCompatActivity as my BaseActivity and android.app.DialogFragment as my Fragment).

What works is either of the two following lines:

getDialog().setCanceledOnTouchOutside(true);

OR

this.getDialog().setCanceledOnTouchOutside(true);

inside onActivityCreated like

@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
    }

What not to use:

DialogFragment.getDialog().setCanceledOnTouchOutside(false);

throws following error

enter image description here

And writing the code in onCreateView crashes the App!
Please update the answer if you find something wrong.

盛夏已如深秋| 2024-12-26 23:31:31

如果您想在点击 DialogFragment 外部时执行某些逻辑,只需重写 onCancel 方法即可。

override fun onCancel(dialog: DialogInterface) {
    super.onCancel(dialog)
    // Do your work here
}

If you want to execute some logic when clicking outside of a DialogFragment, just override the onCancel method.

override fun onCancel(dialog: DialogInterface) {
    super.onCancel(dialog)
    // Do your work here
}
请远离我 2024-12-26 23:31:31

这对于 Java 来说效果很好:

DialogFragment.getDialog().setCanceledOnTouchOutside(false);

This works fine for Java:

DialogFragment.getDialog().setCanceledOnTouchOutside(false);
拍不死你 2024-12-26 23:31:31

我建议仅在尝试上述解决方案后才使用我的解决方案。我在此处描述了我的解决方案。简而言之,我正在检查 DialogFragment.getView() 的触摸边界。当触摸点位于 DialogFragment 之外时,我将关闭该对话框。

I would recommend to use my solution only after trying out above solutions. I have described my solution here. Just to brief, I am checking touch bounds of DialogFragment.getView(). When touch points are outside DialogFragment, I am dismissing the Dialog.

空心空情空意 2024-12-26 23:31:31
            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;
            }
    }
            Dialog.SetCanceledOnTouchOutside(true);

Worked for me
My Code

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;
            }
    }
ま柒月 2024-12-26 23:31:31
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) 
{
        DialogConfermationdialogBinding binding = DialogConfermationdialogBinding.inflate(inflater,container,false);
        View view = binding.getRoot();

        getDialog().setCanceledOnTouchOutside(false);

        return view;
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) 
{
        DialogConfermationdialogBinding binding = DialogConfermationdialogBinding.inflate(inflater,container,false);
        View view = binding.getRoot();

        getDialog().setCanceledOnTouchOutside(false);

        return view;
}
遥远的绿洲 2024-12-26 23:31:30
DialogFragment.getDialog().setCanceledOnTouchOutside(true);

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

DialogFragment.getDialog().setCanceledOnTouchOutside(true);

Must be called in onCreateView (as Apurv Gupta pointed out).

云归处 2024-12-26 23:31:30
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       ...
       getDialog().setCanceledOnTouchOutside(true);
       ... 
       }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       ...
       getDialog().setCanceledOnTouchOutside(true);
       ... 
       }
携余温的黄昏 2024-12-26 23:31:30
    /** 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;
    }
    /** 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文