Reputation: 8768
I have three radiobuttons in a radiogroup. How can I tell Java to do a different thing depending on which button is selected? I have the group and all of the buttons declared:
final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize);
final RadioButton small = (RadioButton)findViewById(R.id.RBS);
final RadioButton medium = (RadioButton)findViewById(R.id.RBM);
final RadioButton large = (RadioButton)findViewById(R.id.RBL);
I know I would say something like this:
if (size.getCheckedRadioButtonId().equals(small){
} else{
}
But equals is not the correct syntax... How can I ask java which button is selected?
Upvotes: 1
Views: 2418
Reputation: 6205
int selected = size.getCheckedRadioButtonId();
switch(selected){
case R.id.RBS:
break;
case R.id.RBM:
break;
case R.id.RBL:
break;
}
Upvotes: 1
Reputation: 3449
Because getCheckedRadioButtonId()
returns an integer, you are trying to compare an integer with RadioButton object. You should compare the id of small
(which is R.id.RBS
) and getCheckedRadioButtonId()
:
switch(size.getCheckedRadioButtonId()){
case R.id.RBS: //your code goes here..
break;
case R.id.RBM: //your code goes here..
break;
case R.id.RBL: //your code goes here..
break;
}
Upvotes: 1
Reputation: 13474
Try:
if (size.getCheckedRadioButtonId() == small.getId()){
....
}
else if(size.getCheckedRadioButtonId() == medium.getId()){
....
}
Upvotes: 1