我一直在研究这个问题,但我找不到一个带有复选框的get drawable解决方案。我想要做的是,当你点击这个特定的复选框时,它会过滤相应颜色的复选框。顺便说一下,这是一个片段。因此,我的复选框可以是红色、黄色或绿色的。如果用户点击它们,它就会改变颜色。例如,我有一个黄色的复选框,它是chkProgress,当它被点击时,我想找到所有黄色的复选框,并过滤黄色的复选框。因此,我有可绘制的I,并且我需要获得可绘制的I,以便能够找到该颜色的复选框。我希望能够在用户单击chkProgress时过滤黄色的复选框。所以,下面是我的代码:
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;
}因此,我正在尝试获得可绘制的,并将它们与该颜色的复选框进行匹配。我找不到任何对我有帮助的东西。任何帮助都将不胜感激。谢谢!
发布于 2017-06-21 02:42:38
我发现了问题所在。因此,我创建了一个方法来检查复选框的计数是否为某个数字,就像我对checked方法所做的那样。我会将该数字的复选框设置为可见或不可见。
public int colorCheckYellow(CheckBox checkbox, int value){
if (value == 1)
checkbox.setVisibility(View.VISIBLE);
else
checkbox.setVisibility(View.GONE);
return value;
}然后我这样叫它:
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()) {
countStart = colorCheckYellow(chkStart, countStart);
countDownload = colorCheckYellow(chkDownload, countDownload);
}else if (!chkInProgess.isChecked()) {
checkboxVisible();
}
}我也有一些其他的代码来使一切工作,但仅此而已!
https://stackoverflow.com/questions/44658878
复制相似问题