我一直在研究這一點,並且我找不到帶有複選框的get drawable的解決方案。我試圖做的是當你點擊這個特定的複選框時,它會過濾相應顏色的複選框。順便提一下,這是一個片段。所以,我有可以是紅色,黃色或綠色的複選框。如果用戶點擊它們,它會改變顏色。例如,我有一個黃色的複選框,它是chkProgress,當它被點擊時,我想查找所有黃色的複選框並過濾黃色的複選框。所以,我有可繪製的ID,我需要讓drawable能夠找到那個顏色的複選框。我希望能夠在用戶點擊chkProgress時篩選黃色的複選框。所以,這裏是我的代碼:Android - 如何通過點擊帶有多個複選框的複選框來過濾帶有可繪製複選框
private CheckBox [] checkBoxes = new CheckBox[2];
checkBoxes[0] = chkStart;
checkBoxes[1] = chkDownload;
private Drawable yellowDrawable = getResources().getDrawable(R.drawable.custom_yellow_checkbox);
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()){
//something here
if (chkStart.getDrawable == yellowDrawable) // need proper code here or a for loop to do this efficiently.
} else if (!chkInProgess.isChecked()){
chkInProgess.setChecked(true);
}
}
});
這裏是我需要找到,如果他們是黃色的複選框:
chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countStart = checked(chkStart, countStart);
editor.putInt(CHECK_START_ID, countStart);
editor.apply();
}
});
chkDownload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countDownload = checked(chkDownload, countDownload);
editor.putInt(CHECK_DOWNLOAD_ID, countDownload);
editor.apply();
}
});
下面是檢查複選框的方法,如果他們是一個特定的顏色:
public int checked(CheckBox checkBox, int count){
if (checkBox.isChecked()){
count++;
} else if (!checkBox.isChecked()){
checkBox.setChecked(true);
count++;
}
if (count == 1){
checkBox.setButtonDrawable(R.drawable.custom_yellow_checkbox);
}
if (count == 2){
checkBox.setButtonDrawable(R.drawable.custom_green_checkbox);
}
if (count > 2){
count = 0;
checkBox.setButtonDrawable(R.drawable.custom_red_checkbox);
}
return count;
}
這裏是方法,當我恢復應用程序:
@Override
public void onResume() {
super.onResume();
start = mSharedPreferences.getInt(CHECK_START_ID, 0);
start = idChecked(chkStart, start);
countStart = start;
download = mSharedPreferences.getInt(CHECK_DOWNLOAD_ID, 0);
download = idChecked(chkDownload, download);
countDownload = download;
}
所以,我試圖獲取drawables並將它們匹配到那種顏色的複選框。我找不到任何能幫助我的東西。任何幫助將非常感激。謝謝!
在模型中具有狀態表示是否更有意義? – snowCrabs