mat*_*d91 5 c# visual-studio xamarin.android xamarin
我想在我的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();
Run Code Online (Sandbox Code Playgroud)
为了做一个随机的例子,假设我想保持对话框打开,直到项目被删除,但我想在Android运行时禁用"是"按钮并更改消息文本.这可能来自我必须访问对话框并更改它的代码吗?senderAlert和args都没有任何有用的属性或方法.
我一直在寻找其他方法来构建我的对话框,我已经看到了这两个:
1)这家伙正在使用下面的方式,但我的DialogInterface没有.OnClickListener()
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do stuff to dialog
}
});
Run Code Online (Sandbox Code Playgroud)
2)这家伙正在使用IDialogInterfaceOnClickListener,我一直试图找到一个如何以这种方式做到的例子,但我还没找到.好像他正在使用null而不是我想要的代码.
.setPositiveButton("OK", (Android.Content.IDialogInterfaceOnClickListener)null)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我使用这样的东西:
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
}
Run Code Online (Sandbox Code Playgroud)
例如:https://wordpress.com/read/feeds/35388914/posts/1024259222
| 归档时间: |
|
| 查看次数: |
9128 次 |
| 最近记录: |