简体   繁体   English

Android 将背景颜色与颜色进行比较

[英]Android Compare Background Color with Color

i am trying to make a little game.我正在尝试制作一个小游戏。

There the Background color Randomly change from Blue to Green and back to Blue.那里的背景颜色随机从蓝色变为绿色,然后再变为蓝色。 If the user click the Blue "Button" then he lose.如果用户点击蓝色“按钮”,那么他就输了。

My problem is how do i get the Background Color?我的问题是如何获得背景颜色? and Compare it with R.color.colorGreen并将其与 R.color.colorGreen 进行比较

I have tried some Examples from here but nothing works.我从这里尝试了一些示例,但没有任何效果。

if(Integer.parseInt(button.getBackground().toString()) == R.color.colorBlue)

You should see this SO post .你应该看到这个 SO 帖子

If you're on Android 3.0+ you can get the color value如果您使用的是 Android 3.0+,则可以获得颜色

ColorDrawable buttonColor = (ColorDrawable) button.getBackground(); int color = buttonColor.getColor();

So, your revised if statement would be因此,您修改后的if语句将是

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
int color = buttonColor.getColor();
if (color == getResources().getColor(R.color.colorBlue)) {
    // if statement body
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM