Android 如何在阵列颜色中设置按钮背景?
为什么自定义按钮结果为灰色?蓝色、绿色和红色实际上是红色、蓝色和红色。我正在尝试从colors.xml中随机选择红蓝绿颜色中的按钮颜色Android 如何在阵列颜色中设置按钮背景?,android,button,user-interface,Android,Button,User Interface,为什么自定义按钮结果为灰色?蓝色、绿色和红色实际上是红色、蓝色和红色。我正在尝试从colors.xml中随机选择红蓝绿颜色中的按钮颜色 public void CustomButton(int btnId) { Button btn = (Button) findViewById(btnId); int[] btnColor = { R.color.blue, R.color.green, R.color.red }; Random random = new Rando
public void CustomButton(int btnId) {
Button btn = (Button) findViewById(btnId);
int[] btnColor = { R.color.blue, R.color.green, R.color.red };
Random random = new Random();
int c = btnColor[random.nextInt(btnColor.length)];
btn.setBackgroundColor(c);
}
除非使用自定义颜色,否则请尝试删除“r”:
public void CustomButton(int btnId) {
Button btn = (Button) findViewById(btnId);
int[] btnColor = {Color.BLUE, Color.GREEN, Color.RED};
Random random = new Random();
int c = btnColor[random.nextInt(btnColor.length)];
btn.setBackgroundColor(c);
}
这将使用内置的Android类。除非您使用自定义颜色,否则请尝试删除“r”:
public void CustomButton(int btnId) {
Button btn = (Button) findViewById(btnId);
int[] btnColor = {Color.BLUE, Color.GREEN, Color.RED};
Random random = new Random();
int c = btnColor[random.nextInt(btnColor.length)];
btn.setBackgroundColor(c);
}
这将使用内置的Android类。试试这个
你应该使用
getResources().getColor(yourcolorid)
获取颜色
代码中的小改动
改变这个
btn.setBackgroundColor(c);
对此
btn.setBackgroundColor(getResources().getColor(c));
试试这个
你应该使用
getResources().getColor(yourcolorid)
获取颜色
代码中的小改动
改变这个
btn.setBackgroundColor(c);
对此
btn.setBackgroundColor(getResources().getColor(c));
我不想使用Color.BLUE Color.GREEN Color.ORANGE,我正在尝试通过随机调用colors.xml中的颜色。请请解决它。我不想使用Color.BLUE Color.GREEN Color.ORANGE我正在尝试通过随机调用colors.xml中的颜色。请解决它。愉快地解决。非常感谢。但我想知道为什么?您的代码和我的代码之间的差异。?@CingSianDal,因为您分配的是由R生成的颜色ID,但颜色的值已解决。非常感谢。但我想知道为什么?您的代码和我的代码之间的差异。?@CingSianDal因为您分配的是由R生成的颜色ID,但颜色的值我不确定您想做什么,但如果您想随机选择按钮颜色,我想这可能会帮助您-->我不确定您想做什么,但是如果你想随机选择按钮的颜色,我想这可能会帮助你-->