猿问

Java 如何自动选中下面的复选框?

我有 25 个复选框。我不想检查层次结构。如果我选中 checkbox2,它应该选中 checkbox2 和 checkbox1。如果我选中 checkbox3,它应该选中 checkbox3,2 和 1....


我已经有一个工作代码。但它太渴望 25 个复选框了。我怎样才能使这个代码更小?


        if (checkBox0Value) {

        checkBox0.setChecked(true);

    } else {

        checkBox0.setChecked(false);

    }

    if (checkBox1Value) {

        checkBox1.setChecked(true);

        checkBox0.setChecked(true);

    } else {

        checkBox1.setChecked(false);

    }

    if (checkBox2Value) {

        checkBox2.setChecked(true);

        checkBox1.setChecked(true);

        checkBox0.setChecked(true);


      .......................

我的 XML 有标签 insinde


<TableRow

            android:id="@+id/tablelvl0"

            android:layout_width="wrap_content"

            android:layout_height="match_parent"

            android:layout_marginLeft="8dp"

            android:layout_marginRight="8dp"

            android:gravity="center">


            <CheckBox

                android:id="@+id/Lcheck0"

                android:layout_width="70dp"

                android:layout_height="wrap_content"

                android:fontFamily="sans-serif-condensed"

                android:gravity="center"

                android:tag="0"

                android:text="0"

                android:textColor="@color/colorAccent"

                android:textSize="14sp"

                android:textStyle="bold" />

                ..............


胡子哥哥
浏览 260回答 3
3回答

杨__羊羊

你可以使用一个数组和一个 idpublic void checkUnCheck(boolean checkBoxValue,int checkBoxId){&nbsp; &nbsp; if(checkBoxValue) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <= checkBoxId; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myCheckBoxs[i].setChecked(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; myCheckBoxs[checkBoxId].setChecked(false);&nbsp; &nbsp; }}并打电话checkUnCheck(checkBox0Value,0);checkUnCheck(checkBox1Value,1);checkUnCheck(checkBox2Value,2);

红颜莎娜

试试这个方法在所有复选框中提供一个标签,而不是像下面这样的 id布局文件&nbsp;<CheckBox&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:tag="0"/><CheckBox&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:tag="1"/>....<CheckBox&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:tag="25"/>活动.java在 Activity 中使用 findViewWithTag 查找所有 CheckBox 视图并在所有设置侦听器并在侦听器事件上处理如下CheckBox cbox;&nbsp; &nbsp; &nbsp;cbox = (CheckBox) findViewWithTag("0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isChecked){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int loopValue = (int) buttonView.getTag();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<loopValue;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBox cb = (CheckBox) findViewWithTag(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.setChecked(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

海绵宝宝撒

一种解决方案是使用HashMap和它的反向HashMap<CheckBox,Integer> map = new HashMap();map.put(checkBox0,0);map.put(checkBox1,1);map.put(checkBox2,2);map.put(checkBox3,3);map.put(checkBox4,4);map.put(checkBox5,5);...map.put(checkBox25,25);Map<String, Character> reverseMap = new HashMap<>();for(Map.Entry<Character, String> entry : map.entrySet()){&nbsp; &nbsp; reverseMap.put(entry.getValue(), entry.getKey());}public void checkValue(CheckBox checkbox, boolean checkBoxValue){&nbsp; &nbsp; if(checkBoxValue)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for(int i=map.get(checkbox); i>=0; i--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reverseMap.get(i).setChecked(true);&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; checkbox.setChecked(false);&nbsp;}称呼checkValue(checkBox20,checkValue);
随时随地看视频慕课网APP

相关分类

Java
我要回答