dot42/xamarin Android 编程和应用 AlertDialog 的 SetOnCancelListene

本文关键字:应用 AlertDialog SetOnCancelListene 编程 xamarin Android dot42 | 更新日期: 2023-09-27 18:31:16

我尝试用C#(VS2010 + dot42/mono)"开发"一个Android应用程序,该应用程序显示大量od消息,获取用户输入并创建AlertDialogs。问题是:1. 您知道如何捕获从Android警报对话框返回的OnCancel事件吗?2. 如何检测究竟是哪个警报对话框发送的?

为了更清楚,我正在尝试让 AlertDialog.Builder SetOnCancelListener(IDialogInterface_IOnCancelListener onCancelListener) 工作。

我创建了一个显示简单消息框的方法,并尝试以某种方式捕获(但到目前为止都是错误的)onCancel 事件。请看下文。你可以帮我吗?

private void button2_OnClick(object sender, EventArgs eventArgs)
    {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
        a_builder.SetMessage("Is this all?");
        a_builder.SetTitle("Question");
        a_builder.SetPositiveButton("Yes", OnMsgClick_Result2 );
        a_builder.SetNegativeButton("Not yet" OnMsgClick_Result2);
        a_builder.SetCancelable(true);
        a_builder.SetOnCancelListener(
                  new IDialogInterface_IOnCancelListener(
                  new IDialogInterface_IOnClickListener(IDialogInterface dialog) { 
                  switch (dialog.which) //<--------- ???
                     {
                     case _dialogA:
                     text1.settext("DialogA was canceled");
                     break;
                     case _dialogB:
                     text1.settext("DialogB was canceled");
                     break;
                     default:
                     text1.settext("Nothing has been canceled");
                     break;
            }
           })
           ); //<--- ??? ERROR HERE of course 
       a_builder.Create().Show();
    } 

dot42/xamarin Android 编程和应用 AlertDialog 的 SetOnCancelListene

您只能检测对话框本身正在关闭。因此,对于使用 AlertDialog.Builder 创建的每个对话框,您都需要有一个单独的CancelListener .

编辑:

像这样的事情就可以了:

var builder = new AlertDialog.Builder(this);
builder.SetTitle("Herp");
builder.SetPositiveButton("Derp", (sender, args) => { /* do stuff on OK */ });
builder.SetNegativeButton("Durr", (sender, args) => { /* do stuff on No */ });
builder.SetCancelable(true);
builder.SetOnCancelListener(new MyCancelListener());
builder.Show();
public class MyCancelListener 
    : Java.Lang.Object, IDialogInterfaceOnCancelListener
{
    public MyCancelListener(/* you could pass stuff here */)
    {
    }
    public void OnCancel(IDialogInterface dialog)
    {
        //Do stuff when cancelled
        //Maybe with stuff from ctor
    }
}