我正在像这样在我的 Activity 布局中以编程方式(因为单选组和单选按钮的数量是变量)添加单选组:
rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.setId(list_questions.get(i).getQuizz_questions_id()); // get from the DB
lrg.add(rg);
for (int j = 0; j < list_answers.size(); j++) {
rb = new RadioButton(this);
rb.setId(list_answers.get(j).getQuizz_answers_id());
rb.setText(String.valueOf(list_answers.get(j).getQuizz_answers_id()) + "-" + String.valueOf(list_answers.size()) + "-" + String.valueOf(list_questions.get(i).getQuizz_questions_id())); // I added that to see if there was a problem on the indexes, but there's not
rg.addView(rb, new LayoutParams(100,LayoutParams.WRAP_CONTENT));
}
tr.addView(rg, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
完整的代码可以在这里找到:http://pastebin.com/d9zZjmuu
我的问题是我可以在三个单选组中选择两个单选按钮。
我知道你很难提供帮助,因为你没有所有的资源等等。但我已经搜索了两天以来的问题是什么,但没有找到任何东西,所以如果你能告诉我是否如果您知道问题出在哪里,我们将不胜感激!
感谢并抱歉我的英语不好。
最佳答案
我重新创建了你的问题并通过给每个 RadioButton 一个唯一的 id 来解决它,简单的像:
rb.setId(list_answers.get(j).getQuizz_answers_id() + i * list_questions.size());
我不确定为什么此时默认的 OnCheckedChangeListeners 变得困惑,但这里有一个简单的解决方法。
关于android - 多个RadioButton可以在一个RadioGroup android中被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365903/