Just want to start off saying I am fairly new to coding for android but I am trying my best.
What I'm trying to do is have multiple results happen when multiple check boxes are checked. its kind of hard to explain the code might help a bit more
public class MainActivity extends AppCompatActivity{
Checkbox cb1, cb2;
Button b1, b2, b3, b4, b5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (findViewById(R.id.cb1);
cb2 = (findViewById(R.id.cb2);
b1 = (findViewById(R.id.b1);
b2 = (findViewById(R.id.b2);
b3 = (findViewById(R.id.b3);
b4 = (findViewById(R.id.b4);
b5 = (findViewById(R.id.b5);
}
@Override
public void onCheckedChanged (CompoundButton buttonView,boolean isChecked) {
if (cb1.isChecked()) {
b1.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
b2.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
b3.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
}
if (cb2.isChecked()) {
b3.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
b4.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
b5.setBackgroundColor(ContextCompat.getColor(this, R.color.grey));
}
else {
b1.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
b2.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
b3.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
b4.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
b5.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
}
}
so basically I'm trying to get it so if only Checkbox 1 is checked, we see the background of buttons 1-3 change and if only checkbox 2 is pushed then buttons 3-5 are changed, and if both are pushed then all buttons are changed. This I am achieving no issue.
The problem I'm having is trying to get them to revert. so for example say I have both cb1&2 checked then I uncheck cb2 the field stays as if cb1&2 are pushed, I'm trying to get it so when cb2 is unchecked the result for only having cb1 checked comes back (buttons 4 & 5 would change back to green)
I've checked all over the internet for this and cant find a answer or at lease a path to point me in the right direction.
I tried to use a "switch/case" set up which didn't work (found out why after) then I tried to loop the switch, again didn't work. I tried using a else if statement then I found out that's basically the same thing as a switch I thought about maybe using a string array but I'm not 100% sure if that would have the desired result.
I still have my code from the failed switch/case attempt if that is needed
Aucun commentaire:
Enregistrer un commentaire