如何比较背景图像资源TextView和R.drawable.bg_image进行切换 [英] How do I compare a background Image resource TextView with a R.drawable.bg_image for switch

查看:74
本文介绍了如何比较背景图像资源TextView和R.drawable.bg_image进行切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语不好.

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屋!

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