Java 如果条件为检查按钮的背景色

Java 如果条件为检查按钮的背景色,java,android,Java,Android,及 我已经设置了按钮的背景色,但当我检查它不起作用时如果您确定背景资源是一种颜色,则可以使用此颜色: buttons[2][2].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { buttons[2][2].setBackgroundDrawable(new PaintDrawable(Color.Y


我已经设置了按钮的背景色,但当我检查它不起作用时

如果您确定背景资源是一种颜色,则可以使用此颜色:

        buttons[2][2].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttons[2][2].setBackgroundDrawable(new PaintDrawable(Color.YELLOW));
            PaintDrawable drawable = (PaintDrawable) buttons[2][2].getBackground();
            color = drawable.getPaint().getColor();

        }
    });
如果您的目的是获取背景色的当前值,则可以将颜色值设置为“标记入”按钮

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
int colorValue = buttonColor.getColor();
然后获取标签:

String colorValue = "#....."
button.setTag(colorValue);

扩展你的描述。你们想实现什么,什么不起作用?按钮的背景色设置成功,但现在我想用条件来检测哪个按钮是彩色的。如果我使用的条件不起作用,这不是问题所在。我已经正确设置了按钮的颜色,但当我检查哪个按钮是彩色的,如果条件不起作用
String colorValue = "#....."
button.setTag(colorValue);
String colorCode = (String)buton.getTag();