It seems to be there is no easy way to get an Alert dialog to return a simple value.
This code does not work (the answer variable cannot be set from within the listener, in fact it does not even compile)
public static boolean Confirm(Context context) {
boolean answer;
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Confirmation");
dialog.setMessage("Choose Yes or No");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
answer = true;
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
answer = false;
}
});
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.show();
return answer;
}
注意: 方法必须是自我约束的, 也就是说, 它并不取决于变量或外部构造 。 只要称呼它, 并获得您的答案, 不管是真实的还是虚假的 。
那么,要做什么?这个简单的愿望是返回“false < true ”或“farse 似乎比它应得的要复杂得多。
此外,设定按钮方法的形式如下:
dialog.setButton(int buttonId, String buttonText, Message msg)
但是,不清楚如何使用它,哪里是迷因被送到哪去,谁被使用,谁被使用?