Radio button cannot be unchecked
I have created a radio button
in my app. Now when radio button
isChecked I call a function, and it's working fine. But my problem is that once I checked the radio button
then and again click of that radio button
doesn't make it unchecked.
So, how that can be done?
My code:
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_frm);
rb1=(RadioButton)findViewBy开发者_开发问答Id(R.id.option1);
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean arg1) {
// TODO Auto-generated method stub
if(rb1.isChecked() == true)
t1.setText("Selected is : "+rb1.getText());
if(rb1.isChecked() == false)
t1.setText("Selected is : ");
}
});
t1=(TextView)findViewById(R.id.TextView01);
}
You can't do that with a radio button. Go for CheckBox. Try this
you can use boolean Flag option ..here is the code
boolean flag = false;
rb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(flag){
rb.setChecked(false);
flag = false;
}
else{
rb.setChecked(true);
flag = true;
}
}
});
精彩评论