2013-12-22 97 views
-1

有人能告訴我我在哪裏做錯了嗎?爲什麼即使將RadioButtos放入RadioGroup中,也可以選擇這兩種RadioButtos?如何動態創建RadioButtons

下面的代碼:

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

您需要先Radiobutton添加到RadioGroup,然後纔打電話

rb_no.setChecked(true); 
+0

_____________thank你 – ioann