【问题标题】:Setting dialog button clicklistener in Android Xamarin在 Android Xamarin 中设置对话框按钮点击侦听器
【发布时间】:2016-05-25 07:36:07
【问题描述】:

我想在我的 Xamarin Android 应用程序 (C#) 中显示一个对话框警报,并且我想在单击按钮时对对话框执行一些操作。

从以前开始,我使用这个代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .SetTitle("Delete")
    .SetMessage("Are you sure you want to delete?)
    .SetPositiveButton("No", (senderAlert, args) => { })
    .SetNegativeButton("Yes", (senderAlert, args) => {
    DatabaseHelper.Delete(item);
});
builder.Create().Show();

举一个随机的例子,假设我想保持对话框打开直到项目被删除,但我想禁用“是”按钮并在 Android 工作时更改消息文本。这可能来自我必须访问对话框并更改它的代码吗? senderAlert 和 args 都没有任何有用的属性或方法。

我一直在寻找其他方法来构建我的对话,我已经看到了这两个:

1) This guy 正在使用下面的方式,但是我的 DialogInterface 没有 .OnClickListener()

builder.setPositiveButton("Test", 
new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        //Do stuff to dialog
    }
});

2) This guy 正在使用 IDialogInterfaceOnClickListener,我一直在尝试找到如何以这种方式执行此操作的示例,但我没有找到任何示例。似乎他使用的是 null 而不是我想要的代码。

.setPositiveButton("OK", (Android.Content.IDialogInterfaceOnClickListener)null)

有什么想法吗?

【问题讨论】:

    标签: c# visual-studio xamarin xamarin.android


    【解决方案1】:

    我使用这样的东西:

          using (var builder = new AlertDialog.Builder(Activity))
          {
             var title = "Please edit your details:";
             builder.SetTitle(title);
             builder.SetPositiveButton("OK", OkAction);
             builder.SetNegativeButton("Cancel", CancelAction);
             var myCustomDialog = builder.Create();
    
             myCustomDialog.Show();
          }
    
          private void OkAction(object sender, DialogClickEventArgs e)
          {
             var myButton = sender as Button; //this will give you the OK button on the dialog but you're already in here so you don't really need it - just perform the action you want to do directly unless I'm missing something..
             if(myButton != null)
             {
                 //do something on ok selected
             }
          }
          private void CancelAction(object sender, DialogClickEventArgs e)
          {
             //do something on cancel selected
          }
    

    示例:https://wordpress.com/read/feeds/35388914/posts/1024259222

    【讨论】:

    • 这看起来和我现在使用的差不多。我的问题是当我写“sender”时,我只能访问“Equals”、“GetHashCode”、“GetType”和“ToString”。在视觉工作室。我需要投射它还是什么? PS:无法访问链接,需要登录。
    • 谢谢! :) var dialog = 作为 AlertDialog 的发送者;正是我想要的:)
    【解决方案2】:

    你可以使用这个类

    using Android.App;
    using System.Threading.Tasks;
    
    public class Show_Dialog
    {
        public enum MessageResult
        {
            NONE = 0,
            OK = 1,
            CANCEL = 2,
            ABORT = 3,
            RETRY = 4,
            IGNORE = 5,
            YES = 6,
            NO = 7
        }
    
        Activity mcontext;
        public Show_Dialog(Activity activity) : base()
        {
            this.mcontext = activity;
        }
    
    
        /// <summary>
        /// Messbox function to show a massage box
        /// </summary>
        /// <param name="Title">to show Title for your messagebox</param>
        /// <param name="Message">to show Message for your messagebox</param>
        /// <param name="result">to get result for your messagebox; OK=1,   Cancel=2,   ingnore=3,   else=0</param>
        /// <param name="SetInverseBackgroundForced">to Set Inverse Background Forced</param>
        /// <param name="SetCancelable">to set force message box is cancelabel or no</param>
        /// <param name="PositiveButton">to show Title for PositiveButton</param>
        /// <param name="NegativeButton">to show Title for NegativeButton</param>
        /// <param name="NeutralButton">to show Title for your NeutralButton</param>
        /// <param name="IconAttribute">to show icon for your messagebox</param>
        /// <returns></returns>
        public Task<MessageResult> ShowDialog(string Title, string Message, bool SetCancelable = false, bool SetInverseBackgroundForced = false, MessageResult PositiveButton = MessageResult.OK, MessageResult NegativeButton = MessageResult.NONE, MessageResult NeutralButton = MessageResult.NONE, int IconAttribute = Android.Resource.Attribute.AlertDialogIcon)
        {
            var tcs = new TaskCompletionSource<MessageResult>();
    
            var builder = new AlertDialog.Builder(mcontext);
            builder.SetIconAttribute(IconAttribute);
            builder.SetTitle(Title);
            builder.SetMessage(Message);
            builder.SetInverseBackgroundForced(SetInverseBackgroundForced);
            builder.SetCancelable(SetCancelable);
    
            builder.SetPositiveButton((PositiveButton != MessageResult.NONE) ? PositiveButton.ToString() : string.Empty, (senderAlert, args) =>
                {
                    tcs.SetResult(PositiveButton);
                });
            builder.SetNegativeButton((NegativeButton != MessageResult.NONE) ? NegativeButton.ToString() : string.Empty, delegate
            {
                tcs.SetResult(NegativeButton);
            });
            builder.SetNeutralButton((NeutralButton != MessageResult.NONE) ? NeutralButton.ToString() : string.Empty, delegate
            {
                tcs.SetResult(NeutralButton);
            });
    
            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
            });
    
            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                builder.Show();
            });
    
    
            // builder.Show();
            return tcs.Task;
        }
    }
    

    你可以使用异步或同步功能

    private void a()
    {
        Show_Dialog msg = new Show_Dialog(this);
        msg.ShowDialog("Error", "Message");
    }
    

    private async void b()
    {
        Show_Dialog msg1 = new Show_Dialog(this);
        if (await msg1.ShowDialog("Error", "Message", true, false, Show_Dialog.MessageResult.YES, Show_Dialog.MessageResult.NO) == Show_Dialog.MessageResult.YES)
        { 
            //do anything
        }
    }
    

    【讨论】:

      【解决方案3】:
      Android.App.AlertDialog.Builder alertDilog = new Android.App.AlertDialog.Builder(this);
      
              alertDilog.SetTitle("simple alert");
              alertDilog.SetMessage("simple message");
              alertDilog.SetNeutralButton("OK", delegate
              {
                  alertDilog.Dispose();
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 2011-08-24
        • 1970-01-01
        • 2011-10-20
        • 2018-11-21
        相关资源
        最近更新 更多