0

有这样的代码:

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?

4

2 回答 2

1

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

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

当您需要获取 id 时,您只需:

int id = imgView1.getTag();
于 2012-09-30T15:30:51.320 回答
0

先保存:

int Id=randImage();
imgView1.setImageResource(Id); 
于 2012-09-30T15:16:32.577 回答