【发布时间】:2016-02-04 13:38:35
【问题描述】:
我想通过str数组随机设置按钮文本:
String str[] = { "1", "2", "3", "4", "5", "6" };
Button buttons[] = new Button[4];
buttons[0] = (Button) findViewById(R.id.id1);
buttons[1] = (Button) findViewById(R.id.id2);
buttons[2] = (Button) findViewById(R.id.id3);
buttons[3] = (Button) findViewById(R.id.id4);
for (int i = 0; i < 4; i++) {
Random r = new Random();
int x = r.nextInt(str.length);
buttons[i].setText(str[x]);
}
【问题讨论】:
-
例如,构建一个 ArrayList 并将使用的字符串添加到其中。然后检查......
-
不重复什么?
-
尝试移动随机 r = new Random();到 for 循环之前。
-
这个问题没有任何细节可以保证答案。