FindViewById循环 [英] FindViewById Loop

查看:146
本文介绍了FindViewById循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试,并把这个变成一个循环,但我似乎无法使R.id.imageView相匹配。我想:字符串ID =R.id.imageView+我在for循环,但它不匹配参数findViewById。有任何想法吗?大大AP preciate任何帮助。

 键[0] =(ImageView的)this.findViewById(R.id.imageView1);
   按钮[1] =(ImageView的)this.findViewById(R.id.imageView2);
   键[2] =(ImageView的)this.findViewById(R.id.imageView3);
   按钮[3] =(ImageView的)this.findViewById(R.id.imageView4);
   按钮[4] =(ImageView的)this.findViewById(R.id.imageView5);
   按钮[5] =(ImageView的)this.findViewById(R.id.imageView6);
   按钮[6] =(ImageView的)this.findViewById(R.id.imageView7);
   按钮[7] =(ImageView的)this.findViewById(R.id.imageView8);
   按钮[8] =(ImageView的)this.findViewById(R.id.imageView9);
   按钮[9] =(ImageView的)this.findViewById(R.id.imageView10);
   按钮[10] =(ImageView的)this.findViewById(R.id.imageView11);
   按钮[11] =(ImageView的)this.findViewById(R.id.imageView12);
 

解决方案

取一个整数数组R.id.imageView1 ...... 12,并通过它的价值 像

 私人整数[] Imgid = {R.id.imageview1,...,12};
 

和使用数组

I want to try and put this into a loop, but I can't seem to make the R.id.imageView to match. I tried : string id = "R.id.imageView"+i inside a for loop but it doesnt match the parameters for findViewById. Any ideas? greatly appreciate any help.

   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);

解决方案

Take One Integer array for R.id.imageView1......12 and pass its value Like

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

and use array

这篇关于FindViewById循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆