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
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(radiobutton1.isChecked()) {
fall.setVisibility(View.GONE);
trip.setVisibility(View.GONE);
illness.setVisibility(View.GONE);
} else if(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:
RadioGroup categoryGroup = (RadioGroup) findViewById(R.id.category_group);
categoryGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(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' checked
break;
case R.id.radioButtonconcern:
// 'Concern' checked
break;
}
}
});
Post a Comment for "How To Attach A Listener To A Radio Button"