11
我是Xamarin新手,我不知道如何在c#中執行以下操作。點擊Positive/Negative按鈕時,我想阻止alertdialog關閉。我需要首先對輸入進行驗證。如果輸入正確,對話框可以關閉,否則我將顯示帶有指示的消息。基本上,我有以下代碼:Xamarin C# - Android的 - 防止AlertDialog關閉PositiveButton點擊
private void CreateAddProjectDialog() {
//some code
var alert = new AlertDialog.Builder (this);
alert.SetTitle ("Create new project");
alert.SetView (layoutProperties);
alert.SetCancelable (false);
alert.SetPositiveButton("Create", HandlePositiveButtonClick);
alert.SetNegativeButton("Cancel", HandelNegativeButtonClick);
}
private void HandlePositiveButtonClick (object sender, EventArgs e) {
//Do some validation here and return false (prevent closing of dialog) if invalid, else close....
}
現在,我紅StackOverflow上以下職位:How to prevent a dialog from closing when a button is clicked
我認爲下面的代碼(從線程所)有解決方案,但我不知道如何重寫我的C#代碼來實現Java:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test for preventing dialog close");
builder.setPositiveButton("Test",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do nothing here because we override this button later to change the close behaviour.
//However, we still need this because on older versions of Android unless we
//pass a handler the button doesn't get instantiated
}
});
AlertDialog dialog = builder.create();
dialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
如何在c#中編碼?特別是setPositiveButton區域中的覆蓋部分...
啊,非常感謝你的確,正是我一直在尋找,我只是不知道如何寫語法明智的。 –
非常感謝您的幫助,您只需要一段時間就能找到辦法。 –