如何动态创建单选按钮

时间:2013-12-22 12:39:03

标签: android dynamic radio-button buttongroup

有人可以告诉我,我在哪里犯了错误吗?为什么即使我将RadioButtos放入RadioGroup也可以选择它们?

以下是代码:

final Dialog dialog = new Dialog(context);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
dialog.setTitle("Title");

RadioButton rb_yes = new RadioButton(context);
rb_yes.setText("yes");

RadioButton rb_no = new RadioButton(context);
rb_no.setText("no");
rb_no.setChecked(true);

RadioGroup radioGroup = new RadioGroup(context);
radioGroup.setOrientation(RadioGroup.VERTICAL);
radioGroup.addView(rb_yes);
radioGroup.addView(rb_no);

ll.addView(radioGroup);

// "context" I've already created before (Context context = this)

1 个答案:

答案 0 :(得分:1)

您需要先将Radiobutton添加到RadioGroup,然后再调用

rb_no.setChecked(true);
相关问题