Android 在单选组的两个线性布局中选择单选按钮不工作

Android 在单选组的两个线性布局中选择单选按钮不工作,android,radio-button,radio-group,Android,Radio Button,Radio Group,我想做下面的图片 我试图设计使用线性布局,但其网络在广播组。 在广播组只有两个方向垂直或水平,所以我如何才能做到这样 它应该在同一个广播组中,因为它可以选择相同的问题 提前谢谢。试试这个 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent

我想做下面的图片

我试图设计使用线性布局,但其网络在广播组。 在广播组只有两个方向垂直或水平,所以我如何才能做到这样

它应该在同一个广播组中,因为它可以选择相同的问题

提前谢谢。

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 

    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />


</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />


</RadioGroup>

</LinearLayout>
调用监听器OnCreate()

  • 声明一个int“mCurrentId”类型的字段,该字段保存选中的单选按钮id
  • 声明一个int数组以保存所有单选按钮ID
  • 在xml中随意放置单选按钮,但给每个单选按钮一个id
  • 在YourActivity.onCreate中:

    setUpRadioButtons();
    
    在您的活动中:

      //fields
      int[] myRadioButtons;
      int currentCheckedRadioButton = 0;
    
      //setUpRadioButtons method
      private void setUpRadioButtons() {
        myRadioButtons= new int[6];
        myRadioButtons[0] = R.id.first;
        myRadioButtons[1] = R.id.second;
        //..
        for (int radioButtonID : myRadioButtons) {
            findViewById(radioButtonID).setOnClickListener(
                        new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentCheckedRadioButton != 0)
                        ((RadioButton) findViewById(currentCheckedRadioButton)).setChecked(false);
                    currentCheckedRadioButton = v.getId();
    
                }
            });
        }
    }
    

    你必须使用两个不同的单选组,并在代码中进行处理,以确保两个组中只有一个单选按钮可以选择。你可以在另一篇文章中查看我的回答。你可以在另一篇文章中查看我的回答。亲爱的,我只想在一个组中查看它。为什么你想要一个组?这是我的ans选项,因为我只需要选中一个选项
    setUpRadioButtons();
    
      //fields
      int[] myRadioButtons;
      int currentCheckedRadioButton = 0;
    
      //setUpRadioButtons method
      private void setUpRadioButtons() {
        myRadioButtons= new int[6];
        myRadioButtons[0] = R.id.first;
        myRadioButtons[1] = R.id.second;
        //..
        for (int radioButtonID : myRadioButtons) {
            findViewById(radioButtonID).setOnClickListener(
                        new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentCheckedRadioButton != 0)
                        ((RadioButton) findViewById(currentCheckedRadioButton)).setChecked(false);
                    currentCheckedRadioButton = v.getId();
    
                }
            });
        }
    }