我正在尝试从一种预定义的数组颜色更改按钮的颜色。像这样:
String[] colors = {"#2962ff", "#00bfa5", "#ff6d00", "#aa00ff"};
Random random = new Random();
String myRandString = random.nextInt(colors.length);
// My Buttons
inGridLayout00.setBackgroundColor(myRandString);
inGridLayout01.setBackgroundColor(myRandString);
inGridLayout10.setBackgroundColor(myRandString);
inGridLayout11.setBackgroundColor(myRandString);
但我不知道如何解决这个问题。
您做得很好,但是您需要将随机数传回数组并获取该随机索引号的颜色
int[] colors = {#2962ff, #00bfa5, #ff6d00, #aa00ff};
Random random = new Random();
int myRandString = random.nextInt(colors.length);
// My Buttons
inGridLayout00.setBackgroundColor(Color.parseColor(colors[myRandString]));
inGridLayout01.setBackgroundColor(Color.parseColor(colors[myRandString]));
inGridLayout10.setBackgroundColor(Color.parseColor(colors[myRandString]));
inGridLayout11.setBackgroundColor(Color.parseColor(colors[myRandString]));
你所要做的就是
setBackgroundColor(colors[myRandString]);
获取随机索引号的颜色
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句