简体   繁体   English

如何将背景图像资源 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.对不起,我的英语不好。

I have one TextView and it have a Background image resource.我有一个 TextView,它有一个背景图像资源。 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.但是当我单击此 TextView 时,我需要 Android 将当前背景图像资源与可绘制文件夹中的 R.drawable.bg_images 之一进行比较。

bg_image_1 bg_image_2 bg_image_1 bg_image_2

if the TextView setBackgroundImageResource is bg_image_1 and i click on it, switch TextView background image to gb_image_2 resource.如果 TextView setBackgroundImageResource 是 bg_image_1 并且我点击它,将 TextView 背景图像切换到 gb_image_2 资源。

how?如何?

thanks谢谢

You can't get a drawable's resource id after setting this drawable as a background.将此可绘制对象设置为背景后,您将无法获取可绘制对象的资源 ID。 But you can store some kind of flag or even a resource id somewhere outside your TextView or maybe in its tag field.但是您可以在TextView之外的某个地方存储某种标志甚至资源 ID,或者可能在其标签字段中。 In this case you'll be able to get it and compare with another id.在这种情况下,您将能够获取它并与另一个 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM