我想创建一个带有两个按钮"is"和“否”的 DialogAlert。我想在 bool 变量中捕获对话框的结果。假设用户单击"is"按钮,那么 DialogResult 应该返回一个真值,如果用户单击“否”按钮,那么 DialogResult 应该返回一个假值。请帮我解决这个问题。提前致谢。
最佳答案
我会使用 AlertDialog ( see documentation here )。如果您有自己的 DialogResult 类,代码可能如下所示:
DialogResult result = new DialogResult();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked Yes: set the value to your result class here. E.g:
MyActivity.result.setValue(true);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked No: set the value to your result class here. E.g:
MyActivity.result.setValue(false);
}
});
AlertDialog alert = builder.create();
关于android - 如何在android中获取DialogResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5945908/