首页 > 解决方案 > 如何从按钮中获取颜色?

问题描述

我需要在 Activity 上找到红色按钮并为这些按钮设置橙色背景颜色。

当我单击按钮时,我将其设置为红色:

view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));

当我单击另一个按钮时,红色按钮应变为橙色。

public void Active(View view){
    for (View but : buttons) {
        but.setClickable(true);
            but.setBackgroundTintList();
    }
}

我不知道如何获取颜色的 id

标签: android

解决方案


对我来说不清楚你的问题,但我会尽力回答。

假设按钮是 a List<Button>,那么您可以做的是。

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);

推荐阅读