我正在使用带有2个单选按钮的对话框来让用户对对象进行分类。如果用户明确单击其中一个单选按钮,则此方法有效,但如果用户只是直接按OK即可。我该如何处理这种情况?
case DIALOG_OBJECT_CLASSIFICATION:
return new AlertDialog.Builder(DrawNewPlans.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("What Object are you drawing?")
.setSingleChoiceItems(R.array.types_of_object, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
makeToast("WhichButton = "+whichButton);
switch (whichButton) {
case 0:
makeToast("User Picked Room");
isRoom = true;
isFurniture = false;
return;
case 1:
makeToast("User Picked Furniture");
isRoom = false;
isFurniture=true;
return;
}
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
makeToast("classOfRoom is "+classOfRoom);
makeToast("isRoom = "+isRoom);
makeToast("isFurniture = "+isFurniture);
if (isRoom == true && isFurniture == false) {
classOfRoom="Room";
} else if (isRoom == false && isFurniture == true) {
classOfRoom="Furniture";
}
/* User clicked Yes so do some stuff */
isClassified=true;
methodWhichStartsDialogs();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
/* User clicked No so do some stuff */
}
}).create();
}
我刚刚添加了Toasts进行调试。非常感谢。
答案 0 :(得分:1)
默认情况下,您可以将一个单选按钮设置为true,即使用户未选择任何内容,默认情况下也会选择该选项。
或者,否则,您可以在用户按下时检查是否选择了至少一个单选按钮。如果未选择任何一个,请不要关闭该对话框。