3 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個贊
你可以使用一個數(shù)組和一個 id
public void checkUnCheck(boolean checkBoxValue,int checkBoxId){
if(checkBoxValue) {
for (int i = 0; i <= checkBoxId; i++) {
myCheckBoxs[i].setChecked(true);
}
}else{
myCheckBoxs[checkBoxId].setChecked(false);
}
}
并打電話
checkUnCheck(checkBox0Value,0);
checkUnCheck(checkBox1Value,1);
checkUnCheck(checkBox2Value,2);

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個贊
試試這個方法
在所有復(fù)選框中提供一個標(biāo)簽,而不是像下面這樣的 id
布局文件
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="0"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"/>
.
.
.
.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="25"/>
活動.java
在 Activity 中使用 findViewWithTag 查找所有 CheckBox 視圖并在所有設(shè)置偵聽器并在偵聽器事件上處理如下
CheckBox cbox;
cbox = (CheckBox) findViewWithTag("0");
cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
int loopValue = (int) buttonView.getTag();
for(int i=0;i<loopValue;i++){
CheckBox cb = (CheckBox) findViewWithTag(i);
cb.setChecked(true);
}
}
}
});

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
一種解決方案是使用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()){
reverseMap.put(entry.getValue(), entry.getKey());
}
public void checkValue(CheckBox checkbox, boolean checkBoxValue)
{
if(checkBoxValue)
{
for(int i=map.get(checkbox); i>=0; i--)
reverseMap.get(i).setChecked(true);
}
else
checkbox.setChecked(false);
}
稱呼
checkValue(checkBox20,checkValue);
添加回答
舉報