【问题标题】:How to dismiss a DialogFragment when pressing outside the dialog?在对话框外按下时如何关闭 DialogFragment?
【发布时间】:2012-01-14 07:18:42
【问题描述】:

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

dialogFragment.setCanceledOnTouchOutside(true);

打电话,但我在文档中没有看到。

DialogFragment 有可能吗?还是我找错地方了?我尝试在“父”活动中拦截触摸事件,但除了没有收到任何触摸事件之外,它对我来说似乎不正确。

【问题讨论】:

    标签: android fragment dismiss


    【解决方案1】:
    DialogFragment.getDialog().setCanceledOnTouchOutside(true);
    

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

    【讨论】:

    • 必须在onCreateView中调用
    • 如果我不希望它取消而只是关闭怎么办?
    • 如果您正在修改对话窗口,请尝试在 onResume() 回调中添加此代码
    【解决方案2】:
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
           ...
           getDialog().setCanceledOnTouchOutside(true);
           ... 
           }
    

    【讨论】:

    • в моем случае getDialog().setCanceledOnTouchOutside(true); не сработало, сработало getDialog().dismiss();
    • @МаксимФомичёв, в первом случае происходит установка свойства диалога, что его можно закрывать нажатием сна Во втором случае вы сами программно закрываете диалог.
    【解决方案3】:
        /** 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;
        }
    

    【讨论】:

    • 这对我不起作用。根据@Apurv,我必须在onCreateView 中致电setCanceledOnTouchOutside。我应该提到我打电话给setCanceledOnTouchOutside(false)
    【解决方案4】:

    这里有很多答案,但是当对话框打开时应用程序崩溃。 在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 中编写代码会使应用程序崩溃! 如果您发现有问题,请更新答案。

    【讨论】:

    • 这可能是因为您在onCreateDialog() 内部创建了一个对话框,这是一种常用的初始化方式。在这种情况下,onCreateView() 什么都不做,甚至不包含视图。您可以尝试将代码移至onCreateDialog()
    • @CoolMind 哦,这是一个很好的观察。一定会把这个放在我的尝试列表中。谢谢!
    • 我认为,尽管如此,您的回答还是有道理的,并且有 4 人标记了优点。我在不久前也遇到过onCreateDialogonCreateView 的问题。也许我错了,你应该保留onActivityCreated,这是进行额外初始化的好方法(例如,参见stackoverflow.com/a/50734566/2914140)。
    • @CoolMind 很适合你。但我可以注意到,您遇到的问题是由于您的 BottomSheet 实施。如果我同意 BSheet,它可能会有所不同。但是这个DialogFragment 的解决方案确实很有效。
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:
      DialogFragment.getDialog().setCanceledOnTouchOutside(false);
      

      打错了。我有同样的问题。这适用于 Java 和 Mono for android Mono 将是:

      this.getDialog().SetCanceledOnTouchOutside(false);
      

      【讨论】:

      • 但是谁问过关于 Mono 的问题? ://
      【解决方案7】:
                  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;
                  }
          }
      

      【讨论】:

        【解决方案8】:

        我建议仅在尝试上述解决方案后才使用我的解决方案。我已经描述了我的解决方案here。简而言之,我正在检查 DialogFragment.getView() 的触摸边界。当接触点在 DialogFragment 之外时,我将关闭 Dialog。

        【讨论】:

          猜你喜欢
          • 2013-08-13
          • 1970-01-01
          • 2010-10-13
          • 1970-01-01
          • 2013-04-15
          • 2011-11-22
          • 2012-01-08
          • 2017-04-12
          • 1970-01-01
          相关资源
          最近更新 更多