0

我需要实施多项选择题。我必须为多项选择添加单选按钮和文本视图。我需要根据选择的数量动态地实现它。有人能帮我吗

for (Answer answer : answers) {
    LinearLayout linearLayoutRw2 = new LinearLayout(this);
    linearLayoutRw2
            .setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));

    RadioGroup radioGroup = new RadioGroup(
            ShowQuestionsActivity.this);
    linearLayoutRw2.addView(radioGroup);

    RadioButton rb = new RadioButton(ShowQuestionsActivity.this);
    radioGroup.addView(rb);

    TextView ansText = new TextView(ShowQuestionsActivity.this);
    ansText.setText(Html.fromHtml(answer.getAnswerText()));
    linearLayoutRw2.addView(ansText);

    linearLayoutShowQues.addView(linearLayoutRw2);

}
4

1 回答 1

0

试试这样,

首先动态创建一个ListView

    ListView choicelist = new ListView(this);
    choicelist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    choicelist.setAdapter(new ArrayAdapter<String>(this,
                   android.R.layout.simple_list_item_single_choice,
                   android.R.id.text1, your_answer_list));
    choicelist.setSelector(new ColorDrawable(0x0));
    choicelist.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    choicelist.setCacheColorHint(0);
    choicelist.setVerticalFadingEdgeEnabled(false);

最后将此列表添加到您的布局中

linearLayoutShowQues.addView(choicelist);

对于自定义,您可以使用自定义适配器。

希望这会帮助你。

于 2013-11-01T10:44:24.947 回答