Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

android - 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. 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

if the TextView setBackgroundImageResource is bg_image_1 and i click on it, switch TextView background image to gb_image_2 resource.

how?

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...