在 XML 文件中:
<!--...-->
<RadioGroup
android:checkedButton="@+id/radioButton1"
android:id="@+id/radioGroupBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:clickable="false"
android:focusable="false"
android:id="@+id/radioButton1"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
<TextView
android:clickable="false"
android:focusable="false"
android:id="@+id/textView1"
android:text="@string/textView1_Text"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
<LinearLayout
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:clickable="false"
android:focusable="false"
android:id="@+id/radioButton2"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
<TextView
android:clickable="false"
android:focusable="false"
android:id="@+id/textView2"
android:text="@string/textView2_Text"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
<!--more RadioButtons-->
</RadioGroup>
<!--...-->
在onCreate() 过程中为LinearLayouts(不适用于RadioButtons)设置onClick() 侦听器(不要忘记import android.view.View; 和import android.widget.RadioGroup;) :
//...
findViewById(R.id.linearLayout1).setOnClickListener(new View.OnClickListener()
{
public void onClick(final View objView)
{
((RadioGroup) findViewById(R.id.radioGroupBase)).check(findViewById(R.id.radioButton1).getId());
}
});
findViewById(R.id.linearLayout2).setOnClickListener(new View.OnClickListener()
{
public void onClick(final View objView)
{
((RadioGroup) findViewById(R.id.radioGroupBase)).check(findViewById(R.id.radioButton2).getId());
}
});
//more SetOnClickListener() calls for more LinearLayouts ("RadioButtons")
//...
剩下的就是在同一个onCreate() 过程中为RadioGroup 设置一个onCheckedChanged() 侦听器:
//...
((RadioGroup) findViewById(R.id.radioGroupBase)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(final RadioGroup objRadioGroup, final int nCheckedID)
{
if (!objRadioGroup.isShown())
return;
//do something depending on which RadioButton was checked
}
});
//...
为RadioGroup 和LinearLayouts 做一个简单的getter 可能是个好主意(后者需要import android.widget.LinearLayout;):
//...
private RadioGroup Get_radioGroupBase() { return findViewById(R.id.radioGroupBase); }
private LinearLayout Get_linearLayout1() { return findViewById(R.id.linearLayout1); }
private LinearLayout Get_linearLayout2() { return findViewById(R.id.linearLayout2); }
//more getters for more LinearLayouts ("RadioButtons")
//...