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

时间:2016-10-13 04:16:43

标签: 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:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。

我该如何处理?

2 个答案:

答案 0 :(得分:1)

您在RadioButton中只添加了一个RadioGroup,这就是您选择了多个单选按钮的原因。 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);
}

答案 1 :(得分:0)

尝试这样我就可以根据需要制作代码。

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