简体   繁体   中英

How i can get image id from ImageView?

Have code like this:

case R.id.button:
int id=randImage();
imgView1.setImageResource(id);
public int randImage() {
Random rand= new Random();
int randomNumber=rand.nextInt(24)+1;
String randomImage="img"+randomNumber;
int id = getResources().getIdentifier(randomImage, "drawable", getPackageName());
return id;

}
case R.id.imgView1:
Intent i= new Intent(); 
i.putExtra("imgId", id); //IS UNREACHABLE

How i can get drawable id after set ?

Not sure if you can recover it after setting, but you can associate a tag with the component:

int id = randImage();
imgView1.setImageResource(id);
imgView1.setTag(id);

And when you need to get the id you simply:

int id = imgView1.getTag();

Save it first:

int Id=randImage();
imgView1.setImageResource(Id); 

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