如何将背景图像资源TextView与R.drawable.bg_image进行比较以进行切换

Ger*_*ais 4 android textview

对不起,我的英语不好.

我有一个TextView,它有一个背景图像资源.但是当我点击这个TextView时,Android需要将当前背景图像资源与可绘制文件夹中的R.drawable.bg_images之一进行比较.

bg_image_1 bg_image_2

如果TextView setBackgroundImageResource是bg_image_1并且我单击它,则将TextView背景图像切换为gb_image_2资源.

怎么样?

谢谢

Mic*_*ael 13

将此drawable设置为背景后,您无法获得drawable的资源ID.但是你可以在你TextView的标签字段之外的某个地方存储某种标志甚至资源标识.在这种情况下,您将能够获得它并与另一个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);
Run Code Online (Sandbox Code Playgroud)