从资源中随机随机播放文本

时间:2014-09-23 11:45:27

标签: java android random

我如何随机改变这个数组? 基本上我有4个按钮,我想随机更改按钮的文本。 例如:按钮有黑色,红色,黄色,绿色接受,当我点击任何按钮时,我需要随机更改这些文本。

这是我的代码

final int[] name={ R.string.text1,R.string.text2,R.string.text3,R.string.text4};
List<Integer> shuffle = new ArrayList<Integer>(Arrays.asList(name));

for(int i = 0; i <shuffle.Count(); i++)
 b[i].Text = shuffle[i];

1 个答案:

答案 0 :(得分:1)

您可以使用Collections.shuffle(List)方法

final Integer[] name={ 1, 2, 3, 4};
List<Integer> shuffle = new ArrayList<Integer>(Arrays.asList(name));

for(int i = 0; i <shuffle.size(); i++) {
    Collections.shuffle(shuffle);
    for (int j = 0; j < shuffle.size(); j++) {
        System.out.print(shuffle.get(j) + " ");
    }
    System.out.println();
}

在此示例中,输出为:

4 3 1 2 
2 1 4 3 
3 1 2 4 
3 1 2 4