【问题标题】:DialogFragment setCancelable property not workingDialogFragment setCancelable 属性不起作用
【发布时间】:2013-05-10 10:22:29
【问题描述】:

我在一个 android 应用程序中工作,并且正在使用 DialogFragment 来显示一个对话框,我想让该 DialogFragment 不可取消。我已将对话框可取消属性设置为 false,但仍然没有影响。

请查看我的代码并建议我一个解决方案。

public class DialogTest extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_test, container, true);
        getDialog().requestWindowFeature(STYLE_NO_TITLE);
        getDialog().setCancelable(false);

        return view;
    }
 }

【问题讨论】:

  • 而不是 getDialog().setCancelable(false);你应该调用 setCancelable(false);
  • 如果您在对话框边界之外单击,它也必须被关闭?
  • 你在尝试冰淇淋三明治吗?请参考上面提到的链接fantasypublishings.com/morePhpHelp/…

标签: android android-dialogfragment


【解决方案1】:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    getDialog().setCancelable(false);

    return view;
}

你必须直接使用setCancelable(false);而不是getDialog().setCancelable(false);

所以更新后的答案会是这样的

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    setCancelable(false);

    return view;
}

【讨论】:

  • 这真是一个令人难以置信的小费,谢谢。我想知道为什么说 dialog.getWindow().requestFeature(--) 你必须在 getWindow 上“包含对话框”?
  • 这不是小费。一个片段正在包装你的对话框,你必须处理片段而不是对话框本身是正常的;)
  • 如果不覆盖onCreateView,也可以从公共Dialog onCreateDialog(Bundle savedInstanceState)调用setCancelable(false)
  • 不适合我。单击后退按钮时,对话框仍然会被关闭。
  • @Blackbelt 我有一个类似的用例,但在我的情况下,对话框外的触摸不会关闭对话框。我在 DialogFragment 中使用 DatePicker。在您上面的答案中,我会为“R.layout.dialog_test”使用什么?我的完整问题列在这里:stackoverflow.com/questions/59825258/…
【解决方案2】:

使用以下代码段

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string..alert_dialog_two_buttons_title);
    newFragment.setCancelable(false);
    newFragment.show(getFragmentManager(), "dialog");
}

如果您想禁用对话框的外侧触摸,请使用以下代码行

DialogFragment.getDialog().setCanceledOnTouchOutside(true);

【讨论】:

  • 这应该是公认的答案,因为它适用于普通警报对话框片段和自定义对话框片段。
【解决方案3】:

如果您使用警报构建器(并且可能在每种情况下都将对话框包装在 DialogFragment 中)来帮助构建对话框,请不要使用 getDialog().setCancelable(false) 或 Dialog.setCancelable(false) 因为它行不通。 使用 setCancelable(false) 如下代码所示,因为它在官方 android 文档中提到:

public void setCancelable (boolean cancelable)

在 API 级别 11 中添加 控制显示的 Dialog 是否可取消。使用 this 而不是直接调用 Dialog.setCancelable(boolean),因为 DialogFragment 需要基于 this 改变其行为。”

参考:http://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)

public class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_layout, null, false);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle("in case you want use a title").setView(view);

        AlertDialog alert = builder.create();
        // alert.setCancelable(false); <-- dont' use that instead use bellow approach
        setCancelable(false); <-  press back button not cancel dialog, this one works fine
        alert.setCanceledOnTouchOutside(false); <- to cancel outside touch

        return alert;
}

【讨论】:

  • 是的,我尝试了上面的解决方案,即使是选中的但对我不起作用,这个答案是新的,所以需要时间来投票,无论如何谢谢。
  • 这是真的,即使我认为 Android 方面的这种行为很奇怪,因为您明确使用例如 AlertDialog.Builder 来构建对话框,您会认为这些属性会覆盖子类.但我可能在这里遗漏了什么?
  • 我认为对话框行为被包装在一个对话框片段中后不再响应 AlertDialog 或至少在 bigining 时。我认为一切都通过 fragmentDialog 进行。
  • 谢谢!我一直在使用 alert.setCancelable(false) 并且不明白为什么它不起作用。
【解决方案4】:

DialogFragment 中的简单解决方案

二手

dialog.setCanceledOnTouchOutside(false)

【讨论】:

    【解决方案5】:
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        AlertDialog.Builder(activity!!).apply {
            isCancelable = false
            setMessage("Your message")
            // your other adjustments
            return this.create()
        }
     }
    

    为我工作。

    主要是使用isCancelable = false而不是setCancellable(false)
    override fun onCreateDialog()内。

    【讨论】:

      【解决方案6】:
      /**
       * Control whether the shown Dialog is cancelable.  Use this instead of
       * directly calling {@link Dialog#setCancelable(boolean)
       * Dialog.setCancelable(boolean)}, because DialogFragment needs to change
       * its behavior based on this.
       *
       * @param cancelable If true, the dialog is cancelable.  The default
       * is true.
       */
      DialogFragment.setCancelable(boolean cancelable) {
          mCancelable = cancelable;
          if (mDialog != null) mDialog.setCancelable(cancelable);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-30
        • 1970-01-01
        • 2023-04-08
        • 2020-08-19
        • 2019-12-16
        • 2015-04-19
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多