抱歉我的英语不好。
我有一个TextView,它有一个背景图像资源。但我需要在单击此TextView时Android将当前背景图像资源与可绘制文件夹中的R.drawable.bg_images之一进行比较。
bg_image_1 bg_image_2
如果TextView setBackgroundImageResource是bg_image_1并且我点击它,请将TextView背景图像切换为gb_image_2资源。
如何?
感谢
答案 0 :(得分:13)
将此drawable设置为背景后,您无法获得drawable的资源ID。但是,您可以在TextView
之外或其标记字段中的某处存储某种标记或甚至资源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);