java - radio 组不工作和垂直对齐android

标签 java android radio-button radio-group

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
    LinearLayout lll = new LinearLayout(Questionnaire.this);
    lll.setOrientation(LinearLayout.VERTICAL);


    JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
    System.out.println(obj2.get("question"));

    TextView tv = new TextView(Questionnaire.this);

    tv.setText(obj2.get("question").toString());
    lll.addView(tv);




    JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
    Log.d("Reading Answer: ", jsonAnswer + "");

    for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
        JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
        System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
        if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
            TextView tv1 = new TextView(Questionnaire.this);
            tv1.setText(obj3.get("inputname").toString());


            RadioGroup group = new RadioGroup(Questionnaire.this);
            group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL

            if (obj3.get("inputtype").toString().matches("radio")) {

                RadioButton newRadioButton = new RadioButton(Questionnaire.this);
                newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
                newRadioButton.setText(obj3.get("inputname").toString());
                group.addView(newRadioButton);


                lll.addView(group);

            }







        }

    }
    ll.addView(lll);

}

这是我动态添加单选按钮的代码。

问题是当我选择一个选项然后选择另一个选项时,它不会取消选择前一个选项。 当它们都添加到同一个单选组时,为什么它不取消选择第一个选择另一个。当方向设置为水平

时,它不会将单选按钮放置在彼此旁边,而是放置在彼此之上

更新

我移动了我的声明

RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL

现在我正在外面父循环

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我该如何处理?

最佳答案

您在 RadioGroup 中仅添加了一个 RadioButton,这就是您选择许多单选按钮的原因。 RadioGroup 至少需要 2 个 RadioButton 才能选中或取消选中效果。

试试这个,希望能成功。

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());

for (int iii = 0; iii < jsonQuestion.length(); iii++) {
    LinearLayout lll = new LinearLayout(Questionnaire.this);
    lll.setOrientation(LinearLayout.VERTICAL);

    JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
    System.out.println(obj2.get("question"));

    TextView tv = new TextView(Questionnaire.this);
    tv.setText(obj2.get("question").toString());
    lll.addView(tv);

    JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
    Log.d("Reading Answer: ", jsonAnswer + "");

    RadioGroup group = new RadioGroup(Questionnaire.this);
    group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL

   for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
        JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
        System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
        if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
            TextView tv1 = new TextView(Questionnaire.this);
            tv1.setText(obj3.get("inputname").toString());

            if (obj3.get("inputtype").toString().matches("radio")) {
                RadioButton newRadioButton = new RadioButton(Questionnaire.this);
                newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
                newRadioButton.setText(obj3.get("inputname").toString());
                group.addView(newRadioButton);
            }
        }
    }
    lll.addView(group);
    ll.addView(lll);
}

关于java - radio 组不工作和垂直对齐android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40012200/

相关文章:

swift - 快速查看单选按钮和复选框的多个部分按钮操作

java - 在模块 classes.jar 中发现重复的类

android - 谷歌云和 xmpp 服务器

java - 为什么 java.util.list 添加以前的数据,而在添加新数据时这些数据被清除了

Java SSL 套接字不工作

android - 在创建与 SQLite 数据库交互的方法/Activity 时,我应该多注意线程安全?

css - 单选按钮选择器 CSS ONLY pls

javascript - 在 javascript 中使用单选按钮更改图像

java - Web 应用程序的已安装程序/计算机信息

java - 字符数组的默认值