我有六个属于同一问题的 RadioButton,我需要将它们放入某个 RadioGroup 中。但这些RadioButtons是在一些LinearLayouts中。我尝试为每个 RadioButton 放置一个 RadioGroup,但我不知道需要做什么才能使其工作。
tab_layout_a.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tipoPackRadio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Normal"
android:textSize="@dimen/radiobuttons_font_sz" />
<RadioButton
android:id="@+id/tipoPackRadio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Gilda"
android:textSize="@dimen/radiobuttons_font_sz" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.9"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tipoPackRadio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Aged Pack \n(cheese/salve)"
android:textSize="@dimen/radiobuttons_font_sz" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tipoPackRadio4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="0dp"
android:layout_weight="1"
android:text="Fellowship"
android:textSize="@dimen/radiobuttons_font_sz" />
<RadioButton
android:id="@+id/tipoPackRadio5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Fertilizer"
android:textSize="@dimen/radiobuttons_font_sz" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:orientation="horizontal">
<RadioButton
android:id="@+id/tipoPackRadio6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Aged Pack \n(honey)"
android:textSize="@dimen/radiobuttons_font_sz"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
最佳答案
尝试这个解决方案..
class MyActivity extends Activity {
RadioButton _Rone, _Rtwo, _Rthree;
@Override
protected void onCreate(Bundle savedInstanceState) {
//after setContentView(.....)
_Rone = (RadioButton) findViewById(R.id.radio_one);
_Rtwo = (RadioButton) findViewById(R.id.radio_two);
_Rthree = (RadioButton) findViewById(R.id.radio_three);
_Rone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(true);
_Rtwo.setChecked(false);
_Rthree.setChecked(false);
}
});
_Rtwo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(false);
_Rtwo.setChecked(true);
_Rthree.setChecked(false);
}
});
_Rthree.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
_Rone.setChecked(false);
_Rtwo.setChecked(false);
_Rthree.setChecked(true);
}
});
// do code here
}`
通过单击任何单选按钮,其他人的选择将重置。
关于java - 如何将线性布局中的单选按钮放入 RadioGroup 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43311905/