在单选组中采用两个线性布局的单选按钮不起作用

Take Radio buttons in two linear layout in radio group not work

我想制作如下图。

我尝试使用线性布局进行设计,但它的网络在无线电组中。 在无线电组中只有两个方向垂直或水平所以我怎样才能做到这一点。

它应该在同一个广播组,因为它的选项是同一个问题。

提前致谢。

试试这个。

<?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>

Java 代码:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            m_group2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that Whosebug exception(like Vladimir Volodin pointed out)
            m_group2.clearCheck(); // clear the second RadioGroup!
            m_group2.setOnCheckedChangeListener(listener2); //reset the listener
           // Log.e("XXX2", "do the work");

            int chkId1 = m_group1.getCheckedRadioButtonId();
            int chkId2 = m_group2.getCheckedRadioButtonId();
            int realCheck = chkId1 == -1 ? chkId2 : chkId1;

            RadioButton rb = (RadioButton)findViewById(realCheck);
            selected_mode = rb.getText().toString();
            Log.i("Selected_Mode", "" + selected_mode);
        }
    }
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            m_group1.setOnCheckedChangeListener(null);
            m_group1.clearCheck();
            m_group1.setOnCheckedChangeListener(listener1);
            //Log.e("XXX2", "do the work");

            int chkId1 = m_group1.getCheckedRadioButtonId();
            int chkId2 = m_group2.getCheckedRadioButtonId();
            int realCheck = chkId1 == -1 ? chkId2 : chkId1;

            RadioButton rb = (RadioButton)findViewById(realCheck);
            selected_mode = rb.getText().toString();
            Log.i("Selected_Mode", "" + selected_mode);
        }
    }
};

call the listener to OnCreate()

m_group1.setOnCheckedChangeListener(listener1);
m_group2.setOnCheckedChangeListener(listener2);
  1. 声明一个类型为 int "mCurrentId" 的字段,其中包含选中的 radioButton id
  2. 声明一个 int 数组来保存所有单选按钮 ID。
  3. 根据需要在 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();

            }
        });
    }
}