Android 当选中第一个radiobutton组上的radiobutton时,如何使radiobutton组外的radiobutton自动取消选中?

Android 当选中第一个radiobutton组上的radiobutton时,如何使radiobutton组外的radiobutton自动取消选中?,android,radio-button,android-radiogroup,Android,Radio Button,Android Radiogroup,我有一个这样的例子,我有一个表格布局,有4个放射组,每个放射组有2个radiobutton。如何使4个放射组彼此同步。当选中射线组1中的radiobutton时,是否取消选中射线组2中的radiobutton 这是我的XML代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:lay

我有一个这样的例子,我有一个表格布局,有4个放射组,每个放射组有2个radiobutton。如何使4个放射组彼此同步。当选中射线组1中的radiobutton时,是否取消选中射线组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);
    }

}
List单选按钮=新建ArrayList();
单选按钮。添加((单选按钮)findViewById(R.id.Pradio1));
单选按钮。添加((单选按钮)findViewById(R.id.Pradio2));
单选按钮。添加((单选按钮)findViewById(R.id.Pradio3));
用于(单选按钮:单选按钮){
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已选中)ProcessRadioButton单击(buttonView);
}
});
}
专用无效处理RadioButton单击(复合按钮视图){
用于(单选按钮:单选按钮){
if(button!=buttonView)button.setChecked(false);
}
}
但结果并不像我预期的那样。当我在放射组1检查radiobutton时,我在放射组2检查radiobutton。radiobutton组1中的radiobutton被取消选中,但当我想再次检查radiobutton组1中的radiobutton时,radiobutton无法检查,radiobutton组2中的radiobutton也无法检查。 这个问题有解决办法吗?根据我的分析,如果在检查另一个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();
            }
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
列出单选按钮;
列出放射组;
布尔flg=假;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终单选按钮pradio1=(单选按钮)findViewById(R.id.pradio1);
最终单选按钮pradio2=(单选按钮)findViewById(R.id.pradio2);
最终单选按钮pradio3=(单选按钮)findViewById(R.id.pradio3);
最终单选按钮pradio4=(单选按钮)findViewById(R.id.pradio4);
最终RadioButton kradio1=(RadioButton)findViewById(R.id.kradio1);
最终RadioButton kradio2=(RadioButton)findViewById(R.id.kradio2);
最终RadioButton kradio3=(RadioButton)findViewById(R.id.kradio3);
最终RadioButton kradio4=(RadioButton)findViewById(R.id.kradio4);
最终射线组rgroup1=(射线组)findViewById(R.id.rgroup1);
最终射线组rgroup2=(射线组)findViewById(R.id.rgroup2);
最终射线组rgroup3=(射线组)findViewById(R.id.rgroup3);
最终射线组rgroup4=(射线组)findViewById(R.id.rgroup4);
radioButtons=新的ArrayList();
单选按钮。添加(pradio1);
单选按钮。添加(pradio2);
单选按钮。添加(pradio3);
单选按钮。添加(pradio4);
单选按钮。添加(kradio1);
单选按钮。添加(kradio2);
单选按钮。添加(kradio3);
单选按钮。添加(kradio4);
radioGroups=新的ArrayList();
添加(rgroup1);
RadioGroup.add(rgroup2);
添加(rgroup3);
radiogroup.add(rgroup4);
Log.e(“JK->”,“+((RadioGroup)pradio1.getParent()).getId());
用于(单选按钮:单选按钮){
setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
Log.e(“JK->”,“JK”);
processRadioButtonClick(按钮视图);
}
}
});
}
}
专用无效处理RadioButton单击(复合按钮视图){
用于(放射组放射组:放射组){
if(radioGroup.getId()!=((radioGroup)按钮view.getParent()).getId()){
radioGroup.clearCheck();
}
}
}
}
代码中出现了什么错误。 在Android中,无法取消选中
单选按钮
。您试图使用setChecked(false)取消选中
单选按钮
。它在内部类(Android的RadioButton类)中行为不端,它在视觉上取消选中单选按钮,但在逻辑上没有取消选中(我自己从RadioButton行为中看到)。这就是你的代码不起作用的原因

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

这是解决方案

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();
            }
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
列出单选按钮;
列出放射组;
布尔flg=假;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终单选按钮pradio1=(单选按钮)findViewById(R.id.pradio1);
最终单选按钮pradio2=(单选按钮)findViewById(R.id.pradio2);
最终单选按钮pradio3=(单选按钮)findViewById(R.id.pradio3);
最终单选按钮pradio4=(单选按钮)findViewById(R.id.pradio4