【问题标题】:How to validate check boxes in a custom dialog?如何验证自定义对话框中的复选框?
【发布时间】:2012-03-02 18:36:41
【问题描述】:

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

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

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();
    }
});

【问题讨论】:

  • 请添加更多标签(包括编程语言)
  • 我已经在网上搜索过,但我没有找到我的问题的解决方案,请任何人帮助我?

标签: java android eclipse


【解决方案1】:

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

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

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);

【讨论】:

  • 你已经理解我的问题了。非常感谢你帮助我
猜你喜欢
  • 1970-01-01
  • 2019-01-08
  • 2011-07-24
  • 1970-01-01
  • 2013-05-03
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
相关资源
最近更新 更多