【问题标题】:Assigning all check items into a variable将所有检查项分配给一个变量
【发布时间】:2015-10-03 13:22:32
【问题描述】:

是否可以将检查项目中的所有值分配到单个变量中?如果单选按钮有这​​个代码.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" />

如果我检查所有项目并将其分配给变量x 的含义。 x = Facility1 , Facility2Facility3 ?

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**
                                } 

正如您所注意到的,我将单选按钮值分配给一个变量,这一次在程序的最后部分,如果 cbFacilities1-3 的值被选中,它们将分配给一个保存字符串值的变量。

【问题讨论】:

    标签: java android xml checkbox


    【解决方案1】:

    嗯,我认为你可以做一组布尔值来保存答案。

    可能是这样的:

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

    【讨论】:

    • 单选按钮算作复选框吗?
    • 我已经编辑了我的帖子,但遇到了错误,与 String 和 boolean 冲突。如果我使用 rg5 = checkboxAnswers
    • 如果我使用 rg5 = checkboxAnswers 与 String 冲突并且 getChecked 没有获取值,则使用 boolean sir 获取错误。
    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多