如何验证自定义对话框中的复选框?

时间:2012-03-02 18:36:41

标签: java android eclipse

我正在尝试使用一组复选框创建自定义对话框,并且我想验证它们,以便如果用户单击“确定”按钮而不选择其中任何一个,则会向用户显示一条消息,询问他们选择至少一个选择。如果他们有(选择了至少一个选项),则会显示一条消息,说明用户已选中的复选框。

我无法继续验证,是否有人可以帮助我,是否有人对我应该如何验证自定义对话框中的复选框有什么想法?

Button conditions_btn=(Button)findViewById(R.id.conditions_btn);
conditions_btn.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v) {
        final Dialog ConditionsDialog =new Dialog(ProfileView.this);
        ConditionsDialog.setContentView(R.layout.diseases);
        ConditionsDialog.setTitle(" select  your health condition   ");

        DCB1=((CheckBox) ConditionsDialog.findViewById(R.id.CB1));
        DCB2=((CheckBox) ConditionsDialog.findViewById(R.id.CB2));
        DCB3=((CheckBox) ConditionsDialog.findViewById(R.id.CB3));
        DCB4=((CheckBox) ConditionsDialog.findViewById(R.id.CB4));

        Diseses_ok_btn= ((Button) ConditionsDialog.findViewById(R.id.ok_button));
        Diseses_ok_btn.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {
                //validate the check boxes  
                if(  DCB1.isChecked()== false&&DCB2.isChecked()==false&&DCB3.isChecked()==false&&DCB4.isChecked()==false ) {
                    showMessage("  please select  your health condition ");
                } else {    
                // what should i do here to get the check boxes that have been checked ??
                }
            }
        });

        Diseses_cancel_btn=((Button)ConditionsDialog..findViewById(R.id.cancel_button));
        Diseses_cancel_btn.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {
                ConditionsDialog.dismiss();
            }
        });

        ConditionsDialog.show();
    }
});

1 个答案:

答案 0 :(得分:0)

我可能误解了你的问题,但这里有......

我不知道如何访问您的疾病的名称,但我会在您的“验证区块”中执行类似的操作:

String msg = "You have selected:";
if (DCB1.isChecked()) {
    msg += "\n    DCB1"; //or get the disease name
}
if (DCB2.isChecked()) {
    msg += "\n    DCB2"; //or get the disease name
}
if (DCB3.isChecked()) {
    msg += "\n    DCB3"; //or get the disease name
}
if (DCB4.isChecked()) {
    msg += "\n    DCB4"; //or get the disease name
}
showMessage(msg);