我如何从ImageView获取图像ID?

时间:2012-09-30 14:54:13

标签: java android

有这样的代码:

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

如何在设置后获得可绘制ID?

2 个答案:

答案 0 :(得分:1)

不确定您是否可以在设置后恢复它,但您可以将标记与组件关联:

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

当你需要获得身份证时,你只需:

int id = imgView1.getTag();

答案 1 :(得分:0)

先保存它:

int Id=randImage();
imgView1.setImageResource(Id); 
相关问题