Android - 动态单选按钮问题

发布于 2024-11-06 15:24:38 字数 1470 浏览 4 评论 0原文

请帮我解决这个问题,我试图根据用户输入动态地在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残疾 2024-11-13 15:24:38

你的问题是这一行:“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文