How To Attach A Listener To A Radio Button
I have an activity that displays various radiobuttons. The radiobuttons are grouped in radiogroups. I want some of the radiobutton to disappear when a certain radiobutton is checke
Solution 1:
try like this:-
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stubif(radiobutton1.isChecked()) {
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
} elseif(radiobutton2.isChecked()) {
}
}
});
Solution 2:
You can set a listener on a RadioGroup
with setOnCheckedChangeListener
. The onCheckedChanged
callback receives the ID of the newly checked button in the checkedId
parameter.
In your case, just add an ID to your radio group (in order to retrieve it from your code)
<RadioGroup
android:id="@+id/category_group"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
And use the following code:
RadioGroupcategoryGroup= (RadioGroup) findViewById(R.id.category_group);
categoryGroup.setOnCheckedChangeListener(newOnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radioButtonincident:
// 'Incident' checked
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
break;
case R.id.radioButtonaccident:
// 'Accident' checkedbreak;
case R.id.radioButtonconcern:
// 'Concern' checkedbreak;
}
}
});
Post a Comment for "How To Attach A Listener To A Radio Button"