谁给我一个背景图像资源的TextView比较有R.drawable.bg_image的开关给我、图像、背景、资源

2023-09-06 04:34:57 作者:风月不等闲

对不起,我的英语不好。

Sorry for my bad English.

我有一个TextView的,它有一个背景图像资源。但是,我需要当我点击这个TextView的Andr​​oid的比较与一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。但是你可以在某处存储某种标志,甚至是资源ID的的TextView 或者在标签领域之外。在这种情况下,你就可以得到它,并与其他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);