Reputation: 21
I have this 3 Radio buttons, let's assume radio1, radio2, radio3. What i did is if i checked radio1 and radio2, the radio3 will be uncheckable and so on. So i can only check 2 buttons at a time.
Then i made this button that if i clicked it, it will unchecked all the checked radio buttons. Now what my problem is whenever i try to check radio1 and radio2 and clear it. The radio3 still won't be clickable even if the radio1 and 2 aren't checked anymore.
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
Button clear;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondxml);
radio1 = (RadioButton) findViewById(R.id.rd1);
radio2 = (RadioButton) findViewById(R.id.rd2);
radio3 = (RadioButton) findViewById(R.id.rd3);
radio1.setOnCheckedChangeListener(this);
radio2.setOnCheckedChangeListener(this);
radio3.setOnCheckedChangeListener(this);
tv = (TextView) findViewById(R.id.tv);
clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
radio1.setChecked(false);
radio2.setChecked(false);
radio3.setChecked(false);
tv.setText("");
}
});
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(radio1.isChecked() && radio2.isChecked()){
tv.setText("Radio1 and 2 is checked ");
radio3.setClickable(false);
}
if(radio1.isChecked() && radio3.isChecked()){
radio2.setClickable(false);
tv.setText("radio 1 and 3 is checked");
}
if(radio2.isChecked() && radio3.isChecked()){
radio1.setClickable(false);
tv.setText("radio2 and 3 is checked");
}
}
}
Upvotes: 2
Views: 9647
Reputation: 11
Actually, when using an individual radio button as opposed to radio group the key is using radio.isSelected() to find the current state of the button.
This code to my on touch listener fixed a similar problem I was having:
OnClickListener radio_button_click = (new OnClickListener() {
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
if( rb.isSelected()==true) {
rb.setSelected(false);
rb.setChecked(false);
rb.setClickable(true);
}
else{
rb.setSelected(true);
rb.setClickable(true);
}
}
}
Upvotes: 1
Reputation: 5266
You have therefore set radio3
to not be clickable by calling radio3.setClickable(false);
when you selected radio1
and radio2
. Your clear button doesn't reverse this.
I suggest in your onClick(...)
method for your clear OnClickListener
you call
radio1.setClickable(true);
radio2.setClickable(true);
radio3.setClickable(true);
this would stop them being disabled when you clear the selection, bringing them back to the default state.
Upvotes: 3