我有動態創建的單選按鈕。如何將動態創建的單選按鈕設置爲RadioGroup?
LinearLayout linLayRoot = (LinearLayout)dialogView.findViewById(R.id.dialog_layout_root);
RadioGroup radGp = new RadioGroup(this);
linLayRoot.addView(radGp);
for (String dir : dirArray)
{
LinearLayout linLayNew = new LinearLayout(this);
linLayNew.setGravity(0x10);
RadioButton radBut = new RadioButton(this); /// <- this button does not work!
radBut.setText("");
TextView tv = new TextView(this);
tv.setText(dir);
tv.setPadding(10, 0, 20, 0);
ImageView ivs = new ImageView(this);
linLayNew.addView(radBut);
linLayNew.addView(tv);
linLayNew.addView(ivs);
radGp.addView(linLayNew);
}
RadioButton radBut1 = new RadioButton(this); /// <- this button works!
radBut1.setId(11);
radBut1.setText("a1");
radGp.addView(radBut1);
RadioButton radBut2 = new RadioButton(this); /// <- this button works!
radBut2.setId(12);
radBut2.setText("b2");
radGp.addView(radBut2);
radGp.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(getApplicationContext(), String.valueOf(checkedId) , Toast.LENGTH_SHORT).show();
}
});
但你可以從上面的評論看,他們並沒有真正的工作,即好像他們沒有綁定到radGp ......也許它,因爲他們是在一個單獨的linlearlayout?
謝謝!
查看上面的編輯,工作沒有錯誤,但仍然允許用戶選擇多個按鈕。 :/ – Roger
@Roger:我編輯了我的回覆。畢竟,我們似乎必須去手動。 – Samuel