从其他变量值做变量名变量名、变量值

由网友(ミ灬华灯绯伤凉薄人)分享简介:基本上我想要自动执行的过程Basically I'm trying to automate process of mDrawable[0] = this.getResources().getDrawable(R.drawable.img0);mDrawable[1] = this.getResources().g...

基本上我想要自动执行的过程

Basically I'm trying to automate process of

mDrawable[0] = this.getResources().getDrawable(R.drawable.img0);
mDrawable[1] = this.getResources().getDrawable(R.drawable.img1);
mDrawable[2] = this.getResources().getDrawable(R.drawable.img2);
mDrawable[3] = this.getResources().getDrawable(R.drawable.img3);
mDrawable[4] = this.getResources().getDrawable(R.drawable.img4);

一个循环:

for(int i = 0;i<=15;i++)
{       
mDrawable[i] = this.getResources().getDrawable(R.drawable.**img+i**);       
}

那么,如何弥补这一资源名称的 IMG4 从我循环变量

谢谢!

推荐答案

我相信你想实现这个样的例子。

I am sure you want to implement this kind of example.

当你想实现findViewById()在循环中当有与就像反varialbe 1,2,3,4,5等。

This is helpful when you want to implement findViewById() inside a loop when there are many number of views with just a difference like "counter" varialbe 1, 2,3,4,5,etc.

for(int i=0; i<15; i++) 
{
    String imageID = "img" + (i);
    int resID = getResources().getIdentifier(imageID, "drawable", getPackageName());

  /* if you want to reference any widgets like Button, TextView, ImageView, etc. then write below code:

    //int resID = getResources().getIdentifier(imageID, "id", getPackageName());   
  */
}
阅读全文

相关推荐

最新文章