Android 如何防止DialogFragment在触摸外部时被忽略?

Android 如何防止DialogFragment在触摸外部时被忽略?,android,xamarin,modal-dialog,android-dialogfragment,Android,Xamarin,Modal Dialog,Android Dialogfragment,我使用的DialogFragment将显示一个完全定制的视图,没有使用本机按钮。 我使用dialog.SetCanceledOnTouchOutsidetrue将对话框设置为可访问; 当用户接触到外部时,我想运行一些代码来确定是否应该取消对话框。 我的问题是,当一个事件被取消,取消,对话框已经被取消,甚至在超级。取消之前;所以我永远不能绕过解雇 我尝试了很多方法,覆盖已触发片段的OnDismiss和OnCancel,但设置为late,在dialogBuilder和/或对话框本身上设置侦听器未触发

我使用的DialogFragment将显示一个完全定制的视图,没有使用本机按钮。 我使用dialog.SetCanceledOnTouchOutsidetrue将对话框设置为可访问; 当用户接触到外部时,我想运行一些代码来确定是否应该取消对话框。 我的问题是,当一个事件被取消,取消,对话框已经被取消,甚至在超级。取消之前;所以我永远不能绕过解雇

我尝试了很多方法,覆盖已触发片段的OnDismiss和OnCancel,但设置为late,在dialogBuilder和/或对话框本身上设置侦听器未触发或太晚,覆盖OnShow以重置侦听器等

被这件事缠住了,知道吗

PS:使用沙马林

public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this.Activity);
    alertDialogBuilder.SetView(myView);
    AlertDialog dialog = alertDialogBuilder.Create();
    dialog.SetCanceledOnTouchOutside(true);
    return dialog;
}

public override void OnDismiss(IDialogInterface dialog)
{
    //BreakPoint here, already dismissed
    base.OnDismiss(dialog);
}

这应该在onCreateView上调用


这应该在onCreateView上调用


我找到的解决方案是:


只需通过“取消”按钮关闭片段。穿上你的衣服 取消按钮单击


我找到的解决方案是:


只需通过“取消”按钮关闭片段。穿上你的衣服 取消按钮单击


你想达到这个结果吗?如果在对话框外部单击,我们可以在对话框关闭之前检测到单击事件。

您可以创建MyAlertDialog来继承AlertDialog

下面是DialogFragment

单击按钮以推动对话框

private void BtnClick_Click(object sender, System.EventArgs e)
    {
        MyDialog dialog = new MyDialog();
        dialog.Show(SupportFragmentManager, "dialog");
    }

你想达到这个结果吗?如果在对话框外部单击,我们可以在对话框关闭之前检测到单击事件。

您可以创建MyAlertDialog来继承AlertDialog

下面是DialogFragment

单击按钮以推动对话框

private void BtnClick_Click(object sender, System.EventArgs e)
    {
        MyDialog dialog = new MyDialog();
        dialog.Show(SupportFragmentManager, "dialog");
    }

正确的解决方案非常简单:

dialog?.setCanceledOnTouchOutside(false)
从文件:

/**
 * Sets whether this dialog is canceled when touched outside the window's
 * bounds. If setting to true, the dialog is set to be cancelable if not
 * already set.
 * 
 * @param cancel Whether the dialog should be canceled when touched outside
 *            the window.
 */

正确的解决方案非常简单:

dialog?.setCanceledOnTouchOutside(false)
从文件:

/**
 * Sets whether this dialog is canceled when touched outside the window's
 * bounds. If setting to true, the dialog is set to be cancelable if not
 * already set.
 * 
 * @param cancel Whether the dialog should be canceled when touched outside
 *            the window.
 */

现在,您应该使用DialogFragment而不是AlertDialog,因此答案如下:

isCancelable = false

将其放在代码的任何部分,您就为Java设置了setCancelablefalse

,现在您应该使用DialogFragment而不是AlertDialog,因此答案如下:

isCancelable = false


将其放在代码的任何部分,就可以为Java设置setCancelablefalse

您调用dialog.SetCanceledOnTouchOutsidetrue;您想防止DialogFragment在触摸外部时忽略…认真地说,您需要实现什么?我想允许在触摸外部取消,但可以在CreateDialog本身设置dialog时中断它。setOnDismissListener和tryAlready已尝试,OnDismissListener甚至没有使用alertDialogBuilder.SetOnDismissListener New OnDialogDismissListener或dialog.SetOnDismissListener New OnDialogDismissListener激发。实际上,使OnDialogDismissListener激发的唯一方法是在OnShow事件集中激发dialog.SetOnDismissListener和alertDialog.SetOnDismissListener New OnDialogDismissListener;但即使在那里,它也出现得太晚了……您已经调用了dialog.setcanceledontouchidetrue;您想防止DialogFragment在触摸外部时忽略…认真地说,您需要实现什么?我想允许在触摸外部取消,但可以在CreateDialog本身设置dialog时中断它。setOnDismissListener和tryAlready已尝试,OnDismissListener甚至没有使用alertDialogBuilder.SetOnDismissListener New OnDialogDismissListener或dialog.SetOnDismissListener New OnDialogDismissListener激发。实际上,使OnDialogDismissListener激发的唯一方法是在OnShow事件集中激发dialog.SetOnDismissListener和alertDialog.SetOnDismissListener New OnDialogDismissListener;但即使在那里,它也太晚了…我希望能够取消,但我希望能够从代码中断取消只需通过取消按钮关闭片段。我不想要一个取消按钮我想要能够取消,但是我想要能够从代码中断取消只是通过一个取消按钮关闭片段。我不想要取消按钮这正是我的目标。我会尽快尝试,谢谢!它就像一个符咒,我几乎在那里,但错过了旗帜:。谢谢@leonlumsft这正是我的目标。我会尽快尝试,谢谢!它就像一个符咒,我几乎在那里,但错过了旗帜:。谢谢你@LeonLuMsft