Xamarin.Android - 防止 AlertDialog 在 PositiveButton 的使用方法


https://www.coder.work/article/399382

var builder = new AlertDialog.Builder(this);
            View v = LayoutInflater.From(this).Inflate(Resource.Layout.component_alert_checkPackage, null);
            alert_ed = v.FindViewById(Resource.Id.alert_ed_queue_num);
            alert_tv_info = v.FindViewById(Resource.Id.alert_tv_info);
            builder.SetView(v);
            builder.SetCancelable(false);
            builder.SetTitle("温馨提示文本内容");
            builder.SetPositiveButton("确定", (EventHandler)null);
            builder.SetNegativeButton("取消", (EventHandler)null);
            var dialog = builder.Create();
            dialog.Show();

            var yesBtn = dialog.GetButton((int)DialogButtonType.Positive);
            var noBtn = dialog.GetButton((int)DialogButtonType.Negative);
            yesBtn.Click += async (sender, args) =>
            {
                await AleDialogRequestAsync(dialog);
            };
            noBtn.Click += (sender, args) =>
            {
                Finish();
            };
            alert_ed.EditorAction += async (a, e) =>
            {
                if (e.Event == null && e.ActionId != ImeAction.Next) { return; }
                if ((e.Event?.Action == KeyEventActions.Up && e.Event?.KeyCode == Keycode.Enter) || e.ActionId == ImeAction.Next)
                {
                    await AleDialogRequestAsync(dialog);
                }
            };

简单使用

// Build the dialog.
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Click me!");

// Create empty event handlers, we will override them manually instead of letting the builder handling the clicks.
builder.SetPositiveButton("Yes", (EventHandler)null);
builder.SetNegativeButton("No", (EventHandler)null);
var dialog = builder.Create();

// Show the dialog. This is important to do before accessing the buttons.
dialog.Show();

// Get the buttons.
var yesBtn = dialog.GetButton((int)DialogButtonType.Positive);
var noBtn = dialog.GetButton((int)DialogButtonType.Negative);

// Assign our handlers.
yesBtn.Click += (sender, args) =>
{
    // Don't dismiss dialog.
    Console.WriteLine("I am here to stay!");
};
noBtn.Click += (sender, args) =>
{
    // Dismiss dialog.
    Console.WriteLine("I will dismiss now!");
    dialog.Dismiss();
};