获取android中按钮的背景颜色 [英] Get the background color of a button in android

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

问题描述

如何获取按钮的背景颜色.在 xml 中,我使用 ---- android:background = XXXXX 设置背景颜色现在在活动类中,我如何检索它具有的这个值?

How do i get the background color of a button. In the xml i set the background color using ---- android:background = XXXXX now in the activity class how can i retrieve this value that it has ?

推荐答案

很遗憾我不知道如何检索实际颜色.

Unfortunately I don't know how to retrieve the actual color.

很容易将其作为 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.

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

这篇关于获取android中按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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