我需要实施一个多项选择题。我必须添加单选按钮和文本视图以供多种选择。我需要根据选择的数量动态地实现它。有人可以帮助我
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);
}
答案 0 :(得分: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);
对于自定义,您可以使用自定义适配器。
希望这会对你有所帮助。