我需要在执行某段代码之前等待用户确认。这是我构建和显示AlertDialog的地方:
private bool AskForCommand()
{
bool userOK = false;
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.SetPositiveButton("Yes", (sender, args) =>
{
userOK = true;
})
.SetNegativeButton("No", (sender, args) =>
{
userOK = false;
})
.SetMessage("Send vocal command now?")
.SetTitle("System Message");
RunOnUiThread(() =>
{
dialog.Show();
});
return userOK;
}
这是我称之为方法的地方:
if (!string.IsNullOrEmpty(exactSpeech))
{
bool userOK = false;
try
{
userOK = AskForCommand();
}
catch (Exception ex)
{
//TODO: catch
}
if (cfg.InstantSendVocal || userOK)
SendCommandToBoard(exactSpeech);
}
问题是警告是在方法" SendCommandToBoard"之后显示的。
答案 0 :(得分:1)
只需将以下代码放入SetPositiveButton
回调中
dialog.SetPositiveButton("Yes", (sender, args) =>
{
if (cfg.InstantSendVocal)
SendCommandToBoard(exactSpeech);
}
忘掉名为userOK的布尔变量