1

我是 Xamarin Android 的新手。我有一个活动,当按下 textview 时会显示一个警报。AlertDialog 的代码是:

textView1.Click += (sender, e) =>
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetTitle("title");
                alert.SetMessage("Message");
                alert.SetCancelable(false);
                alert.SetPositiveButton("Cerrar Sesión", delegate { funcCerrarSesion(); });
                alert.SetNegativeButton("Salir", delegate { Finish(); });
                alert.SetNeutralButton("Volver", delegate {  });

                RunOnUiThread(() =>
                {
                    alert.Show();
                });
            };

我需要在按下后退按钮时,此事件关闭 AlertDialog。谢谢你。

PD:我是 Visual Studio 2012 + Plugin Xamarin 的开发人员

编辑(ρяσѕρєя K 的解决方案):

    Dialog dialog;
            protected override void OnCreate(Bundle bundle)
            {
..
textView1.Click += (sender, e) =>
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetTitle("Advertencia");
                alert.SetMessage("Está seguro?");
                alert.SetCancelable(false);
                alert.SetPositiveButton("Cerrar Sesión", delegate { funcCerrarSesion(); });
                alert.SetNegativeButton("Salir", delegate { Finish(); });
                alert.SetNeutralButton("Volver", delegate {  });

                RunOnUiThread(() =>
                {
                    dialog = alert.Create();
                    dialog.Show();
                });
            };
...
}

public override void OnBackPressed()
        {
            if (dialog != null)
            {
                if (dialog.IsShowing)
                {
                    dialog.Dismiss();
                }
                else
                {
                    base.OnBackPressed();
                }
            }
            else
            {
                base.OnBackPressed();
            }
        }

这会继续显示警报,但back button按下时,警报不会关闭。

4

1 回答 1

1

我需要按下后退按钮时,此事件关闭 AlertDialog

要在后退键上关闭对话框:

1.覆盖OnBackPressed

2.需要alertOnBackPressedso declare alertobject before中访问对象OnCreate

public override void OnBackPressed()
{
    if (alert !=null){
       if(alert.IsShowing){
          alert.Dismiss ();
        }else{
          base.OnBackPressed();
        }
     }else{
        base.OnBackPressed();
     }
}
于 2015-02-20T18:44:46.307 回答