我想试着把它放到一个循环中,但是我似乎不能使R.id.imageView匹配。我尝试过:在for循环中使用string id = "R.id.imageView"+i,但它与findViewById的参数不匹配。有什么想法吗?非常感谢您的帮助。
buttons[0] = (ImageView) this.findViewById(R.id.imageView1);
buttons[1] = (ImageView) this.findViewById(R.id.imageView2);
buttons[2] = (ImageView) this.findViewById(R.id.imageView3);
buttons[3] = (ImageView) this.findViewById(R.id.imageView4);
buttons[4] = (ImageView) this.findViewById(R.id.imageView5);
buttons[5] = (ImageView) this.findViewById(R.id.imageView6);
buttons[6] = (ImageView) this.findViewById(R.id.imageView7);
buttons[7] = (ImageView) this.findViewById(R.id.imageView8);
buttons[8] = (ImageView) this.findViewById(R.id.imageView9);
buttons[9] = (ImageView) this.findViewById(R.id.imageView10);
buttons[10] = (ImageView) this.findViewById(R.id.imageView11);
buttons[11] = (ImageView) this.findViewById(R.id.imageView12);发布于 2012-05-17 05:00:26
获取R.id.imageView1...12的一个Integer数组,并将其值传递为
private Integer[] Imgid = {R.id.imageview1,....,12 };并使用数组
发布于 2012-05-17 04:59:49
使用getIdentifier()方法:
Resources res = getResources(); //if you are in an activity
for (int i = 1; i < 13; i++) {
String idName = "imageView" + i;
buttons[i] = (ImageView) findViewById(res.getIdentifier(idName, "id, getPackageName()));
}发布于 2015-01-13 20:08:02
和上面一样,只是修复了打字错误。看起来比‘最佳答案’更好,因为如果你不需要的话,你不需要定义任何数组。
Resources res = getResources(); //if you are in an activity
for (int i = 1; i < 13; i++) {
String idName = "imageView" + i;
imageViews[i] = (ImageView) findViewById(res.getIdentifier(idName, "id", getPackageName()));
}https://stackoverflow.com/questions/10626550
复制相似问题