无线电组不工作和垂直对齐 android

radio group not working and vertical alignment android

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);

}

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

问题是当我 select 一个选项然后 select 花药它不会取消 select 前一个选项。 为什么当第一个 select 和另一个 添加到同一个无线电组时,它 select 不取消。此外,当 方向设置为水平

时,它不会将单选按钮并排放置,而是将其放置在彼此之上

更新

我移动了

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

现在我得到

外边父 for 循环

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

我该如何处理?

像这样尝试我有你想要的代码。

     ScrollView scrollView = (ScrollView) findViewById(R.id.templayout);
    LinearLayout mLinearLayout = new LinearLayout(this);
    mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    scrollView.addView(mLinearLayout);
    JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());

    for (int iii = 0; iii < jsonQuestion.length(); iii++) {


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

        TextView tv = new TextView(Questionnaire.this);
        tv.setText(obj2.get("question").toString());
        mLinearLayout.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);
                }
            }
        }
        mLinearLayout.addView(group);
    }
}

您在 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);
}