本文介绍了获取Android的一个按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何得到一个按钮的背景颜色。在XML我使用---- Android的设置背景颜色:背景= XXXXX如今在活动课我怎么能检索此值,它有?
How do i get the background color of a button.In the xml i set the background color using ---- android:background = XXXXXnow in the activity class how can i retrieve this value that it has ?
推荐答案
不幸的是,我不知道如何获取实际的颜色。
Unfortunately I don't know how to retrieve the actual color.
这很容易得到这样一个绘制对象
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。
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的一个按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!