将所有检查项目分配到变量

时间:2015-07-14 18:30:20

标签: java android xml checkbox

是否可以在单个变量中分配来自选中项目的所有值?如果单选按钮具有此代码.getCheckedRadioButtonId(),那么复选框呢。这是我的xml布局的复选框。

<CheckBox
                android:id="@+id/cbFacilities"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Facility1"
                android:textColor="#000000"
                android:textSize="20dp"
                android:textStyle="bold" />
            <CheckBox
                android:id="@+id/cbFacilities2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Facility2"
                android:textColor="#000000"
                android:textSize="20dp"
                android:textStyle="bold" />
            <CheckBox
                android:id="@+id/cbFacilities3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Facility3"
                android:textColor="#000000"
                android:textSize="20dp"
                android:textStyle="bold" />

这意味着我检查所有项目并将其分配给变量xx = Facility1Facility2Facility3

String rg1 = "*";
                                String rg2 = "*";
                                String rg4 = "*";
                                String rg5 = "*";
                                theFilter[5] = "0";
                                if (cbRegAffil.isChecked()) {
                                    int reg = rgRegAffil
                                            .getCheckedRadioButtonId();
                                    RadioButton rBtReg = (RadioButton) dia
                                            .findViewById(reg);
                                    rg1 = (String) rBtReg.getText();

                                }
                                if (cbAdmin.isChecked()) {
                                    int adm = rgAdmin
                                            .getCheckedRadioButtonId();
                                    RadioButton rBtAdm = (RadioButton) dia
                                            .findViewById(adm);
                                    rg2 = (String) rBtAdm.getText();
                                }

                                if (cbAmbience.isChecked()) {
                                    int amb = rgAmbience
                                            .getCheckedRadioButtonId();
                                    RadioButton rBtAmbience = (RadioButton) dia
                                            .findViewById(amb);
                                    rg4 = (String) rBtAmbience.getText();
                                }
                                if (cbTuition.isChecked()) {
                                    theFilter[5] = spin.getSelectedItem()
                                            .toString();
                                }
                                if (cbSpecialty.isChecked()) {
                                    int spec = rgSpecialty
                                            .getCheckedRadioButtonId();
                                    RadioButton rBtSpec= (RadioButton) dia
                                            .findViewById(spec);
                                //  rg5 = (String) rBtSpec.getText();

                                }
                                if (cbFacilities.isChecked()||cbFacilities2.isChecked()||cbFacilities3.isChecked()) {
                //**I want to assign the values checked here with the variable rg5**
                                } 

正如您所注意到我将radiobutton值分配给变量,这次在程序的最后部分,cbFacilities1-3的值如果被检查将分配给保存字符串值的单个变量。

1 个答案:

答案 0 :(得分:0)

嗯,我想你可以做一系列布尔代表来解答答案。

看起来像这样:

boolean[] checkboxAnswers = new boolean[3];
checkboxAnswers[0] = checkbox1.getChecked();
checkboxAnswers[1] = checkbox2.getChecked();
checkboxAnswers[2] = checkbox3.getChecked();