Android 如何从按钮中获取颜色?
我需要找到活动上的红色按钮,并为这些按钮设置橙色背景色 单击按钮时,我将其设置为红色:Android 如何从按钮中获取颜色?,android,Android,我需要找到活动上的红色按钮,并为这些按钮设置橙色背景色 单击按钮时,我将其设置为红色: view.setBackgroundTintList(ColorStateList.valueOf(Color.RED)) 当我点击另一个按钮时,红色按钮应该变成橙色 public void Active(View view){ for (View but : buttons) { but.setClickable(true); but.setBackgroun
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED))代码>
当我点击另一个按钮时,红色按钮应该变成橙色
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
我不知道如何获得颜色的id
but.setBackgroundColor(Color.RED);
你的问题我不清楚,但我会尽力回答
假设按钮是一个列表
,那么您可以做的是
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
当你点击按钮时,使用
button.setBackgroundResource(Color.Red);
这是非常糟糕的做法按钮是什么?Android studio没有(R.colors.orange)颜色我使用的rgbYap,你也可以在colors.xml
文件夹中定义你的颜色:)