如何获得按钮的背景颜色。点击操作取决于背景颜色 [英] How to get background color of button. On click action depends on background color

查看:244
本文介绍了如何获得按钮的背景颜色。点击操作取决于背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从color.xml ....设置背景颜色控制(Button)。
我需要按钮的背景颜色来检查指定的那个颜色。

Set background color to control(Button) from color.xml.... I need background color of button for check which color assigned that time.

推荐答案

使用这样:

很容易把它当做Draw​​able

It's easy to get this as a Drawable

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

如果您知道这是一种颜色,那么您可以尝试

If you know this is a color then you can try

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

如果你使用Android 3.0+,你可以得到颜色的资源id。 / p>

And if you're on Android 3.0+ you can get out the resource id of the color.

int colorId = buttonColor.getColor();

并将其与您指定的颜色进行比较,例如

And compare this to your assigned colors, ie.

if (colorID == R.color.green) {
  log("color is green");
}

这篇关于如何获得按钮的背景颜色。点击操作取决于背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆