初始化多个TextView的替代方法

时间:2018-01-25 20:42:26

标签: android textview initialization

我想初始化多个TextView(已经在布局文件中),而不必在单独的行中(即循环?)。从我在网上看到的情况来看,我知道你不能但只需要一些确认。

TextView t1 = (TextView) findViewById(R.id.t1);
TextView t2 = (TextView) findViewById(R.id.t2);
....
TextView t10 = (TextView) findViewById(R.id.t10);

感谢。

3 个答案:

答案 0 :(得分:1)

以编程方式创建

 for(int i=0;i<3;i++){
     textView = new TextView(ActivityName.this);
     textView.setId(i);
}

答案 1 :(得分:1)

for(int i=0; i< 10; i++) {
    int resID = getResources().getIdentifier("t" + (i+1), "id", getPackageName());
    TextView t = (TextView) findViewById(resID));
    ....
   } 
} 

答案 2 :(得分:0)

使用基本工具没有更快的方法。

但是,您可能会找到感兴趣的Butter Knife注释库。