1

我正在使用带有 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 进行调试。非常感谢。

4

1 回答 1

1

您可以将一个单选按钮默认设置为 true,即即使用户没有选择任何内容,该选项也会默认被选中。

或者,否则,您可以检查当用户按下确定时是否至少选择了一个单选按钮。如果未选择任何内容,请不要关闭对话框。

于 2013-01-26T17:25:43.213 回答