我之前也已经 pst 这个了,但不知何故它被卡住了。所以做一个重复的。
我正在编写一个简单的脚本,其中有一个包含两个选项的确认框。我需要在一项 Activity 中多次调用它。所以我做了一个方法来做到这一点。基于返回的 bool 值,我想编写条件语句。
// Before oncreate
静态 bool 确认;
private void showConfirmation() {
// UserFunctions userFunctions = null;
// TODO Auto-generated methodastub
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ProfileActivity.this);
// set title
alertDialogBuilder.setTitle("test");
// set dialog message
alertDialogBuilder.setMessage("Please update the unfilled fields.").setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
confirmation = false;
}
}).setNegativeButton("Later on", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
confirmation = true;
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
public void myOnClicktest(View v) {
showConfirmation();
if (confirmation){
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
startActivity(intent);
}
}
最佳答案
这样做-
alertDialogBuilder.setMessage("Please update the unfilled fields.").setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
confirmation = false;
}
}).setNegativeButton("Later on", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
startActivity(intent);
}
});
因为在您的情况下,您的代码不会等待确认值在对话框中更改,它会跳过对话框,默认情况下确认值是 false。
关于返回 bool 值的 Android alertdialog 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18556938/