如何随机在Android的按钮的顺序 [英] how to randomize the order of buttons in Android
本文介绍了如何随机在Android的按钮的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我读过有关集合,以随机对象的顺序。就这样,我想尝试随机我的按钮在我的Android应用程序。
I've read about collections to randomize the order of an object. With that, I'd like to try to randomize my buttons in my android application.
的code下面一大块:
Here a chunk of code:
Button[] bObject = new Button[6];
private void getCorrectObject() {
// TODO getCorrectObject
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
// Shuffle the collection
Collections.shuffle(objects);
bObject[objects]; // I'm having trouble implementing the shuffle logic here.
}
所以任何有帮助的回应是真正的AP preciated。谢谢你。
SO any helpful response is truly appreciated. Thanks.
推荐答案
明白了手动解决,然后设法得到它动态地解决了藏汉。有一个很好的想法,你可以用第二个数组列表做到这一点。
got it solved manually and then tried to get it solved dynamically aswell. got a nice idea that you can do it with a second arraylist.
List<Integer> objects = new ArrayList<Integer>();
objects.add(0);
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
// Shuffle the collection
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());
}
这是否帮助你?
does this help you?
编辑: 是的,让我们解决这个聊天。 IM也很好奇如何可以做到这一点!
yes let us solve this in chat. im also curious how this can be done!
这篇关于如何随机在Android的按钮的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文