在代码中一次性设置所有 int / imageresource / clicklistener ext

set all int / imageresource / clicklistener ext in one while code

我有30个整数。和 30 张图像。我想在一个代码中将它们全部设置为相同
像这样(在 Java 和 android )

我的 int 值像 i0、i1、i2

int x = 0 ;
while ( x<30) {
//set ix = 0;
x++;
}

"x starts from 0 to 29"
 ax.setImageResource

如何以编程方式使 int / images 相同

for(int x=0;x<30;x++)
{
   ImageView img=(ImageView)findViewById(getResources().getIdentifier("a"+x, "id", getPackageName()));
   img.setImageResource(getIdentifier("i"+x , "drawable", getPackageName()));
}

编辑

如何为 int 和 onclicklistener 制作它?

通过在循环中执行 img.setOnClickListener(...) 在每个 ImageView 对象上设置 OnClickListener...

对于您的整数,您可以使用数组,但如果它们的变量名很重要,那么您可以尝试这样做:

Map<String,Integer> myints=new HashMap<String,Integer>(){
   {
      put("nameX",0);
      put("nameX",1);
      ...and so on... 
   }
};

然后在循环中执行此操作

int num=myints.get("name"+x);

您可以对其他对象使用相同的方法,只需将 Integer 替换为您需要的对象...