1

これをループに入れたいのですが、R.id.imageViewを一致させることができないようです。試してみました: string id = "R.id.imageView"+i for ループ内ですが、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);
4

3 に答える 3

3

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()));
}
于 2012-05-16T20:59:48.647 に答える
3

R.id.imageView1 ...... 12の1つの整数配列を取得し、その値をLikeに渡します。

private Integer[] Imgid = {R.id.imageview1,....,12  };

配列を使用します

于 2012-05-16T21:00:26.073 に答える
1

上記と同じですが、タイプミスを修正しました。必要がない場合は配列を定義する必要がないため、「Best Answer」よりも優れているようです。

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()));
}
于 2015-01-13T12:08:02.487 に答える