1

我尝试使用 C#(VS2010 + dot42/mono)“开发”一个 Android 应用程序,该应用程序显示大量 od 消息,获取用户输入并创建 AlertDialogs。问题是: 1. 你知道如何捕捉Android AlertDialog(s) 返回的OnCancel事件吗?2.如何检测到底是哪个AlertDialog发送的?

更清楚地说,我试图让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();
    } 
4

1 回答 1

1

您只能检测到对话框本身正在关闭。因此,对于您创建的每个对话框,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
    }
}
于 2013-09-15T09:04:06.307 回答