gpt4 book ai didi

android - 单选按钮的随机 "GROUP"?

转载 作者:行者123 更新时间:2023-11-29 17:54:18 25 4
gpt4 key购买 nike

我怎样才能随机“组”单选按钮?

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());
}

}

上面的代码不是radio button的“GROUP”,怎么变成了RadioGroup?并以随机顺序显示。有人帮忙吗?

最佳答案

这里是一个用单选组创建单选按钮的例子....

 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);

}
});
}

关于android - 单选按钮的随机 "GROUP"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com