简体   繁体   English

在 Android 中选中单选按钮时,如何取消选中单选组中的所有单选按钮?

[英]How to uncheck all radio buttons in a radio group, when a radio button is checked in Android?

I have created programmatically 5 radio groups with 4 radio buttons each.我以编程方式创建了 5 个单选组,每个组有 4 个单选按钮。 The problem is, when i first check a radio button from the first radio group and after that i check the second radio button from the same radio group, the first radio button remains checked.问题是,当我首先检查第一个单选组中的单选按钮,然后检查同一个单选组中的第二个单选按钮时,第一个单选按钮仍保持选中状态。 What can i do to have the normal behavior for all the radio buttons in the radio group?我该怎么做才能使单选组中的所有单选按钮具有正常行为? This is my code:这是我的代码:

    radioGroup = new RadioGroup[5];
    answer = new RadioButton[4];
    int i = 0;
    for (Question qn : questions) {
        radioGroup[i] = new RadioGroup(this);
        int j = 0;
        for (Answer an : answers) {
            if (qn.getID() == an.getQuestion_id_answer()) {
                answer[j] = new RadioButton(this);
                answer[j].setText(an.getAnswer());
                radioGroup[i].addView(answer[j]);
                j++;
            }
        }
        linearLayout.addView(radioGroup[i]);
        i++;
    }

Thanks!谢谢!

"Checking one RadioButton that belongs to a RadioGroup unchecks any previously checked RadioButton within the same group." “检查属于RadioGroup一个RadioButton会取消选中同一组中任何先前检查过的RadioButton 。” Source: developer.android.com来源: developer.android.com

If I understand right: You are talking about 4 RadioButtons in the same RadioGroup that refuse to uncheck themselves when another button in the same RadioGroup gets checked?如果我的理解对不对:你说的是4个RadioButtons在同一RadioGroup拒不取消选中自己,当在同另一个按钮RadioGroup被选中?

If that's the case, just give a different Resource ID to every RadioButton and that should fix it.如果是这种情况,只需为每个 RadioButton 提供不同的Resource ID即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本 - How to get the text from radio button in a radio group when radio button checked state is changed 选中后如何使用凌空发送单选按钮/单选按钮组的数据 - How to send data of radio buttons/radio group using volley when checked 如何设置选中的单选按钮状态。 不使用广播组 - How to set checked radio button state . not using radio group 所有单选按钮都在一个无线电组,android下被选中 - All radio buttons are getting selected under a radio group, android 如何获取从 Android 中的单选组检查的最后一个单选按钮? - How do i get the last radio button that was checked from a radio group in Android? 单选组未设置为Android单选按钮 - Radio group not setting to radio buttons Android 如何在按钮组中添加单选按钮? - How to add radio buttons in button group? 如何检查单选按钮是否选中? - How check radio buttons checked or not? 我有一个广播组,在这个广播组中,我有两个单选按钮。 如何在Android中使单选按钮不可点击? - I have a radio group and in this radio group i have two radio buttons. how can i make Radio Button non-clickable in android? 如何检查按下按钮时选中了哪些单选按钮 - How to check which radio buttons are checked upon pressing a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM