RadioGroup with radio buttons in different layouts
Is it possible to have a radio group with radio buttons in their own layouts开发者_开发技巧? Every radio button has to be on a row that contains text and an image. I wouldn't use a list view because I only have 2 rows.
Here is the way to do it:
Create a separate RadioGroup
on each RadioButton
in the XML layout file. For example:
<RadioGroup
android:id="@+id/rdoFooWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rdoFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RadioGroup
android:id="@+id/rdoBooWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rdoBoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
Now, inside the java source code, do something like this:
rdoFooWrapper = (RadioGroup) findViewById(R.id.rdoFooWrapper);
rdoFoo = (RadioButton) findViewById(R.id.rdoFoo);
rdoBooWrapper = (RadioGroup) findViewById(R.id.rdoBooWrapper);
rdoBoo = (RadioButton) findViewById(R.id.rdoBoo);
rdoFoo.setOnCheckedChangeListener(this); // implement OnCheckedChangeListener to the current class
rdoBoo.setOnCheckedChangeListener(this);
// ...
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
rdoFooWrapper.clearCheck();
rdoBooWrapper.clearCheck();
}
精彩评论