当第一个单选组上的单选按钮被选中时,如何自动取消选中单选组外的单选按钮?

how to make a radiobutton outside the radio group uncheck automatically when radiobutton on first radiogroup is checked?

我有一个这样的案例,我有一个有 4 个单选按钮的表格布局,每个单选按钮有 2 个单选按钮。如何使 4 个无线电组相互同步。当radiogroup 1的radiobutton被选中时,radiogroup 2的radiobutton没有被选中?

这是我的 XML 代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_margin="20sp"
    android:textSize="25sp"
    android:fontFamily="monospace"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/judulkolomtes"/>

<TableLayout
    android:layout_margin="10sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_white">

        <RadioGroup
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/P"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>

        <TextView
            android:padding="10sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/K"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textColor="@color/white"/>
        </RadioGroup>

        <TextView
            android:padding="10sp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/pernyataanhead"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/white"/>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

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

            <RadioButton
                android:id="@+id/Pradio1"
                android:checked="false"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>

            <RadioButton
                android:id="@+id/Kradio1"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan1"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border_black"
        android:orientation="horizontal">

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

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Pradio2"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:layout_gravity="center"
            android:id="@+id/Kradio2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan2"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:id="@+id/Pradio3"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio3"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan3"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/border_black">

        <RadioGroup
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="horizontal">

        <RadioButton
            android:id="@+id/Pradio4"
            android:layout_gravity="center"
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>

        <RadioButton
            android:id="@+id/Kradio4"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/border_black"/>
        </RadioGroup>

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:paddingTop="2dp"
            android:text="@string/pernyataan4"
            android:textSize="19sp"
            android:textColor="@color/black"/>
    </TableRow>
</TableLayout>

我试过使用此代码:

List<RadioButton> radioButtons = new ArrayList<RadioButton>();
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio1) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio2) );
    radioButtons.add( (RadioButton)findViewById(R.id.Pradio3) );

    for (RadioButton button : radioButtons){

        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) processRadioButtonClick(buttonView);
            }
        });
    }

private void processRadioButtonClick(CompoundButton buttonView){

    for (RadioButton button : radioButtons){

        if (button != buttonView ) button.setChecked(false);
    }

}

但结果并不如我所料。当我检查单选组 1 的单选按钮然后我检查单选组 2 中的单选按钮时。单选组 1 中的单选按钮未被选中,但是当我想再次检查单选组 1 中的单选按钮时,无法检查单选按钮,单选组 2 中的单选按钮也是如此. 这个问题有解决办法吗?根据我的分析,在选中另一个radiobutton时将radiobutton设为false,会导致radiobutton无法选中。

这是解决方案。

public class MainActivity extends AppCompatActivity {
    List<RadioButton> radioButtons;
    List<RadioGroup> radioGroups;

    boolean flg = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RadioButton pradio1 = (RadioButton) findViewById(R.id.Pradio1);
        final RadioButton pradio2 = (RadioButton) findViewById(R.id.Pradio2);
        final RadioButton pradio3 = (RadioButton) findViewById(R.id.Pradio3);
        final RadioButton pradio4 = (RadioButton) findViewById(R.id.Pradio4);
        final RadioButton kradio1 = (RadioButton) findViewById(R.id.Kradio1);
        final RadioButton kradio2 = (RadioButton) findViewById(R.id.Kradio2);
        final RadioButton kradio3 = (RadioButton) findViewById(R.id.Kradio3);
        final RadioButton kradio4 = (RadioButton) findViewById(R.id.Kradio4);
        final RadioGroup rgroup1 =  (RadioGroup) findViewById(R.id.Rgroup1);
        final RadioGroup rgroup2 = (RadioGroup) findViewById(R.id.Rgroup2);
        final RadioGroup rgroup3 = (RadioGroup) findViewById(R.id.Rgroup3);
        final RadioGroup rgroup4 = (RadioGroup) findViewById(R.id.Rgroup4);

        radioButtons = new ArrayList<RadioButton>();
        radioButtons.add(pradio1);
        radioButtons.add(pradio2);
        radioButtons.add(pradio3);
        radioButtons.add(pradio4);
        radioButtons.add(kradio1);
        radioButtons.add(kradio2);
        radioButtons.add(kradio3);
        radioButtons.add(kradio4);

        radioGroups = new ArrayList<RadioGroup>();
        radioGroups.add(rgroup1);
        radioGroups.add(rgroup2);
        radioGroups.add(rgroup3);
        radioGroups.add(rgroup4);

        Log.e("JK->",""+ ((RadioGroup) pradio1.getParent()).getId());

        for (RadioButton button : radioButtons) {
            button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Log.e("JK->","JK");
                        processRadioButtonClick(buttonView);
                    }

                }
            });
        }
    }

    private void processRadioButtonClick(CompoundButton buttonView) {
        for (RadioGroup radioGroup: radioGroups){
            if (radioGroup.getId() != ((RadioGroup) buttonView.getParent()).getId()) {
                radioGroup.clearCheck();
            }
        }
    }
}

你的代码出了什么问题。

无法在 Android 中取消选中 RadioButton。您试图使用 setChecked(false) 取消选中 RadioButton。它在内部 类(RadioButton Class of Android) 中行为不端它在视觉上但不符合逻辑地取消选中单选按钮(我自己的虽然来自 RadioButton 行为)。这就是您的代码无法正常工作的原因。

解法说明。

为了解决这个问题而不是使用 setChecked(false) 取消选中 RadioButton,我使用 radioGroup.clearCheck() 取消选中该特定组的两个按钮(视觉上和逻辑上都)。我不确定这是否是一个明确的解释。如果您有任何问题,请告诉我。 :)