如何将背景图像资源 TextView 与 R.drawable.bg_image 进行比较以进行切换 [英] How do I compare a background Image resource TextView with a R.drawable.bg_image for switch
问题描述
对不起,我的英语不好.
Sorry for my bad English.
我有一个 TextView,它有一个背景图片资源.但是当我点击这个 TextView 时,我需要 Android 将当前的背景图像资源与可绘制文件夹中的 R.drawable.bg_images 之一进行比较.
I have one TextView and it have a Background image resource. But i need when i click in this TextView the Android compare the current Background image resource with a one of R.drawable.bg_images in drawable folder.
bg_image_1bg_image_2
bg_image_1 bg_image_2
如果 TextView setBackgroundImageResource 是 bg_image_1 并且我点击它,将 TextView 背景图片切换到 gb_image_2 资源.
if the TextView setBackgroundImageResource is bg_image_1 and i click on it, switch TextView background image to gb_image_2 resource.
怎么做?
谢谢
推荐答案
将drawable设置为背景后,无法获取drawable的资源id.但是您可以在 TextView
之外的某处或它的标记字段中存储某种标志甚至资源 id.在这种情况下,您将能够获取它并与另一个 id 进行比较.
You can't get a drawable's resource id after setting this drawable as a background. But you can store some kind of flag or even a resource id somewhere outside your TextView
or maybe in its tag field. In this case you'll be able to get it and compare with another id.
Object tag = textView.getTag();
int backgroundId = R.drawable.bg_image_2;
if( tag != null && ((Integer)tag).intValue() == backgroundId) {
backgroundId = R.drawable.bg_image_1;
}
textView.setTag(backgroundId);
textView.setBackgroundResource(backgroundId);
这篇关于如何将背景图像资源 TextView 与 R.drawable.bg_image 进行比较以进行切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!