我使用RadioGroup
只实现了一个项目,可以从动态创建的radiobuttons中进行选择。
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)
答案 0 :(得分:1)
rbButton
目前处于另一种观点,这就是例外的原因。您必须致电 parent_of_rbButton.removeView(rbButton)
才能将其添加到rbGroup
。