如何生成的Android随机事件? [英] How to generate random events in android?
本文介绍了如何生成的Android随机事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象的数组,并希望能够随机选择一个从当按钮为pressed名单。你会如何做,在Android的?
I have a an array of objects and would like to be able to randomly choose one from the list when a button is pressed. How would you do that in Android?
推荐答案
做这样的事情你onClickListener在
Do something like this inside your onClickListener
Random rand = new Random();
int selector = rand.nextInt(yourList.length);
yourList.get(selector);
类似的东西。
编辑:其实,如果它是一个ArrayList,那么这将是更喜欢这种
Actually if it is an ArrayList then it will be more like this
Random rand = new Random();
int selector = rand.nextInt(yourList.size());
yourList.get(selector);
这篇关于如何生成的Android随机事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文