当按下中立按钮时阻止Android AlertDialog关闭
问题描述:
我正在使用Xamarin和Nuget的V7支持库。当按下中立按钮时阻止Android AlertDialog关闭
我想覆盖默认的Dialog.Dismiss()
行为。我已经没有运气尝试这样做,在对话框关闭时,中性(刷新)按钮:
更新:我需要的行为是被按压中性按钮后MyDialog
保持打开状态。目前,当按下中性按钮时,refreshMyDialogButton_Click
方法触发,然后MyDialog
被自动解除。
using AlertDialog = Android.Support.V7.App.AlertDialog;
....
protected AlertDialog MyDialog;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
MyDialog = createMyDialog(myDialogView);
MyDialog.Show();
}
protected AlertDialog createMyDialog(View myDialogView)
{
var builder = new AlertDialog.Builder(this);
builder
.SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
.SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)refreshMyDialogButton_Click)
.SetPositiveButton(/**foo**/)
.SetView(/**my custom view**/)
.SetTitle(/**title**/)
.SetMessage(/**message**/)
;
return builder.Create();
}
private void refreshMyDialogButton_Click(object sender, EventArgs e)
{
/** start async tasks but don't close dialog **/
}
更新2
添加以下代码到onCreate
方法,但是这导致一个空引用异常上dialogNeutral
:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null);
MyDialog = createGathererDialog(myDialogView);
var dialogNeutral = MyDialog.GetButton((int)DialogButtonType.Neutral);
dialogNeutral.Click += refreshMyDialogButton_Click;
}
protected AlertDialog createMyDialog(View myDialogView)
{
var builder = new AlertDialog.Builder(this);
builder
.SetNegativeButton("back", delegate { MyDialog.Dismiss(); })
.SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)null)
.SetPositiveButton(/**foo**/)
.SetView(/**my custom view**/)
.SetTitle(/**title**/)
.SetMessage(/**message**/)
;
return builder.Create();
}
答
该按钮必须被实例化为null
然后在.Show()
方法被调用后通过.GetButton()
进行访问。
答
我知道我迟到了这个答案,但你可以在你的对话课上试试这个。
this.Dialog.SetCanceledOnTouchOutside(false);
,并同时从外部访问,
dialogBox.Dialog.SetCanceledOnTouchOutside(false);
有关使用setCancelable(假的)什么? –
澄清 - 您的问题是对话关闭的事实,然后触发事件或关闭它,而不会触发它? – EvilBeer
@EvilBeer感谢您的回复,我已经更新了我的问题以获得清晰度 – tallpaul