如何比较背景图像资源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将当前的Background图像资源与drawable文件夹中的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_1 bg_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.
如何?
谢谢
推荐答案
将可绘制对象设置为背景后,您将无法获得该可绘制对象的资源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屋!