代码...... {
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
ll.addView(rb[i]);
rb[i].setText("Test");
}
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
ll.removeView(rb[i]);
}
ll.removeView(submit);
Questions();
}});
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是出现单选按钮,用户可以选择任何一个.作为初学者,我确定我没有正确设置单选按钮.用户可以选择所有五个按钮,然后一旦选择,他们也无法取消选中它们.用户应该只能从五个中选择一个选项......我怎样才能做到这一点?
mai*_*450 16
您必须将单选按钮添加到RadioGroup,然后将RadioGroup添加到布局
我想念一些像提交的信息,但你的代码应该是这样的:
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
Questions();
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21676 次 |
| 最近记录: |