繁体   English   中英

防止在按下中立按钮时关闭Android AlertDialog

[英]Prevent Android AlertDialog from closing when neutral button is pressed

我正在使用Nuget的Xamarin和V7支持库。

我想覆盖默认的Dialog.Dismiss()行为。 我没有运气尝试过,按下中性(刷新)按钮时对话框关闭:

为清楚起见更新:我需要的行为是在按下中性按钮后MyDialog保持打开状态。 当前,当按下中立按钮时,将refreshMyDialogButton_Click方法,然后自动关闭MyDialog

using AlertDialog = Android.Support.V7.App.AlertDialog;

....
protected AlertDialog MyDialog;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
    MyDialog = createMyDialog(myDialogView);
    MyDialog.Show();
}

protected AlertDialog createMyDialog(View myDialogView)
{
    var builder = new AlertDialog.Builder(this);
    builder
        .SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
        .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)refreshMyDialogButton_Click)
        .SetPositiveButton(/**foo**/)
        .SetView(/**my custom view**/)
        .SetTitle(/**title**/)
        .SetMessage(/**message**/)
        ;
    return builder.Create();
}

private void refreshMyDialogButton_Click(object sender, EventArgs e)
{
    /** start async tasks but don't close dialog **/
}

更新2

我将以下代码添加到onCreate方法中,但这在dialogNeutral上导致Null Reference Exception:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
    MyDialog = createGathererDialog(myDialogView);
    var dialogNeutral = MyDialog.GetButton((int)DialogButtonType.Neutral);
    dialogNeutral.Click += refreshMyDialogButton_Click;
}

protected AlertDialog createMyDialog(View myDialogView)
{
    var builder = new AlertDialog.Builder(this);
    builder
        .SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
        .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)null)
        .SetPositiveButton(/**foo**/)
        .SetView(/**my custom view**/)
        .SetTitle(/**title**/)
        .SetMessage(/**message**/)
        ;
    return builder.Create();
}

这些按钮必须实例化为null ,然后在调用.Show()方法之后通过.GetButton()访问。

我知道我迟到了这个答案,但是您可以在对话框类中尝试一下。

this.Dialog.SetCanceledOnTouchOutside(false);

当从外部进入时

dialogBox.Dialog.SetCanceledOnTouchOutside(false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM