I have a bunch of checkboxes and I have the same method for each checkbox. What I'm trying to do is to have a checkbox if that particular checkbox was checked for the first time. What's happening is I think the counter is still going up even though the checkbox was not checked but others were. So, I'm trying to have the checkboxes go from red to yellow to green (it starts as red), but when one of the checkboxes is checked it turns yellow which is what I want, but if I try to check another checkbox, it will turn green instead of yellow. I tried having a boolean, but I'm not exactly sure how to have it know if that particular checkbox was checked the first time and set the counter to 0. If I set the counter to 0 inside of the onClick method, it will just keep being 0. So, I am wondering if there is a way to know if the checkbox is checked for the first time and have it set to 0. I want to be efficient with my code and to not have a separate counter for each of the checkboxes. I have tried researching this problem but can't find any help. Any help will be much appreciated. Thanks!
public void checked(CheckBox checkBox){
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);
}
}
chkDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checked(chkDownload);
}
});
chkGetCalendar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checked(chkGetCalendar);
}
});
chkStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checked(chkStart);
}
});
Aucun commentaire:
Enregistrer un commentaire