我曾经RadioGroup
只实现一项以从动态创建的单选按钮中进行选择。
final LinearLayout firstRowTxtLayout = new LinearLayout(fContext);
firstRowTxtLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rbGroup.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rbButton = new RadioButton(fContext);
rbButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rbButton.setId(rbTagincreament);
rbGroup.addView(rbButton);
我RadioGroup
已经在循环外初始化了。并将RadioGroup
视图添加到另一个布局
我再次更改了它(如下所示)。现在我得到了单选按钮,但我可以选择组中的每个按钮。
private void createRadioButton(int num) {
Log.i("comVisa", "Num ==" + num);
rg = new RadioGroup(fContext); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
rb = new RadioButton(fContext);
rb.setId(num++);
rg.addView(rb); // the RadioButtons are added to the radioGroup instead
// of the layout
firstRowTxtLayout.addView(rg);// you add the whole RadioGroup to the
// layout
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
}
当我初始化rg = new RadioGroup(fContext);
外部时,我得到:
`IllegalStateException`. You must call removeView() on the child's parent first while using RadioGroup
带有代码行的 Logcat:
03-12 14:05:35.266: W/System.err(32734): at com.vipera.ts.gui.custom.comVisaApprovalList.createRadioButton(comVisaApprovalList.java:531) firstRowTxtLayout.addView(rg);
03-12 14:05:35.266: W/System.err(32734): at com.vipera.ts.gui.custom.comVisaApprovalList.constructRow(comVisaApprovalList.java:459)createRadioButton(rbTagincreament++);
03-12 14:05:35.271: W/System.err(32734): at com.vipera.ts.gui.custom.comVisaApprovalList.createTableLayout(comVisaApprovalList.java:411)
03-12 14:05:35.271: W/System.err(32734): at com.vipera.ts.gui.custom.comVisaApprovalList.init(comVisaApprovalList.java:121)