Android - 动态单选按钮问题
请帮我解决这个问题,我试图根据用户输入动态地在 for 循环中生成 2 个单选按钮,我想要的是单选按钮,例如
*Radio Button1 *RadioButton2
*Radio Button1 *RadioButton2
*Radio Button1 *RadioButton2
*单选按钮1 *单选按钮2
。 。 。
。
等等..取决于循环!
这是我的代码片段,它仅适用于一行中的 2 个单选按钮,但是当我增加 count 的值时。 。我收到 android 运行时错误,radiogroup 子项已经有父项了。 :
List<RadioGroup> allradioGroup = new ArrayList<RadioGroup>();
RadioGroup radioGroup;
List<RadioButton> allRadio = new ArrayList<RadioButton>();
RadioButton radioButton;
for (int i = 0; i < count; i++) {
/* Defining RadioGroup */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
allradioGroup.add(radioGroup);
/* Displaying Radio Buttons */
for (int j = 0; j < 2; j++) {
radioButton = new RadioButton(this);
radioButton.setTextColor(getResources().getColor(R.color.grey));
radioButton.setId((j + 100));
allRadio.add(radioButton);
if (allRadio.get(j).getId() == 100) {
radioButton.setText("private");
} else if (allRadio.get(j).getId() == 101) {
radioButton.setText("public");
}
allradioGroup.get(i).addView(allRadio.get(j), j,
layoutParams);
}
linear.addView(allradioGroup.get(i));
}
请帮忙。谢谢
Please help me out on this one, Im trying to produce 2 radio buttons in a for loop dynamically depending upon the user input waht i want is to have radio buttons like
*Radio Button1 *RadioButton2
*Radio Button1 *RadioButton2
*Radio Button1 *RadioButton2
*Radio Button1 *RadioButton2
.
.
.
.
and so forth .. depending upon the loop !
Here is the snippet from my code its is working for only 2 radio buttons in one row only but when i increase the value of count . . i get android run time error of radiogroup child already having a parent. :S
List<RadioGroup> allradioGroup = new ArrayList<RadioGroup>();
RadioGroup radioGroup;
List<RadioButton> allRadio = new ArrayList<RadioButton>();
RadioButton radioButton;
for (int i = 0; i < count; i++) {
/* Defining RadioGroup */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
allradioGroup.add(radioGroup);
/* Displaying Radio Buttons */
for (int j = 0; j < 2; j++) {
radioButton = new RadioButton(this);
radioButton.setTextColor(getResources().getColor(R.color.grey));
radioButton.setId((j + 100));
allRadio.add(radioButton);
if (allRadio.get(j).getId() == 100) {
radioButton.setText("private");
} else if (allRadio.get(j).getId() == 101) {
radioButton.setText("public");
}
allradioGroup.get(i).addView(allRadio.get(j), j,
layoutParams);
}
linear.addView(allradioGroup.get(i));
}
Please HELP. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是这一行:“allradioGroup.get(i).addView(allRadio.get(j),j,layoutParams);”
当您创建第二行时,参数 j 的范围为 0-1。您正在尝试使用 allRadio.get(j) ,它在第二次运行时将返回您创建的第一个单选按钮(它已经有一个父级)。要解决此问题,请将“j”替换为:“i*2+j”。那应该解决它。
Your problem is this line: "allradioGroup.get(i).addView(allRadio.get(j), j, layoutParams);"
The parameter j will be in the range 0-1, also when you create your second row. You are trying to use allRadio.get(j) which on the second run will return the first radio button you created (which already has a parent). To fix this, replace "j" with: "i*2+j". That should fix it.