我如何随机选择radiobutton?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
Collections.shuffle(objects);
List<Button> buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.button0));
buttons.add((Button)findViewById(R.id.button1));
buttons.add((Button)findViewById(R.id.button2));
buttons.add((Button)findViewById(R.id.button3));
buttons.add((Button)findViewById(R.id.button4));
for (int i = 0; i < objects.size(); i++) {
buttons.get(i).setText(objects.get(i).toString());
}
}
代码不是单选按钮的“GROUP”,它是如何成为RadioGroup的?并以随机顺序显示。有人帮忙吗?
答案 0 :(得分:3)
这是一个创建带有无线电组的单选按钮的例子....
private void createRadioButton() {
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
Collections.shuffle(objects);
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(objects.get(i)+"");
}
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);
}
});
}