简体   繁体   中英

Comparing the id of two views in android?

I have 2 image buttons in my Android project. I have set some images for the buttons and I change those images in the program. I want to check if the two image buttons have the same images, how can I check that??

I tried directly comparing them in an if statement by getting the two ids using getId(), but my program stops responding...

you can try to check the Source of the ImageView , if it is same. you can not do it directly, as there is no method provided by default. but you can try something like this using Tag

in onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);

imageView0.setTag(R.drawable.one);
imageView1.setTag(R.drawable.two);

//you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv) {
    return (Integer) iv.getTag();
}

this is assuming that same images are picked up from same location. if you have same images being picked up from different locations, then you want to compare Bitmaps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM