Findviewbyid()in for循环

时间:2017-10-28 12:22:19

标签: java android for-loop findviewbyid

I have tried this,但我不明白如何为我的变量制作自定义名称。这是我想缩短的代码:

    TextView viewmt1 = (TextView) v.findViewById(R.id.mt1);
    TextView viewmt2 = (TextView) v.findViewById(R.id.mt2);
    TextView viewmt3 = (TextView) v.findViewById(R.id.mt3);
    TextView viewmt4 = (TextView) v.findViewById(R.id.mt4);
    TextView viewmt5 = (TextView) v.findViewById(R.id.mt5);
    TextView viewmt6 = (TextView) v.findViewById(R.id.mt6);
    TextView viewmt7 = (TextView) v.findViewById(R.id.mt7);

    TextView viewtid1 = (TextView) v.findViewById(R.id.tid1);
    TextView viewtid2 = (TextView) v.findViewById(R.id.tid2);
    TextView viewtid3 = (TextView) v.findViewById(R.id.tid3);
    TextView viewtid4 = (TextView) v.findViewById(R.id.tid4);
    TextView viewtid5 = (TextView) v.findViewById(R.id.tid5);
    TextView viewtid6 = (TextView) v.findViewById(R.id.tid6);
    TextView viewtid7 = (TextView) v.findViewById(R.id.tid7);

    ImageView viewImage1 = (ImageView) v.findViewById(R.id.imageView1);
    ImageView viewImage2 = (ImageView) v.findViewById(R.id.imageView2);
    ImageView viewImage3 = (ImageView) v.findViewById(R.id.imageView3);
    ImageView viewImage4 = (ImageView) v.findViewById(R.id.imageView4);
    ImageView viewImage5 = (ImageView) v.findViewById(R.id.imageView5);
    ImageView viewImage6 = (ImageView) v.findViewById(R.id.imageView6);
    ImageView viewImage7 = (ImageView) v.findViewById(R.id.imageView7);

3 个答案:

答案 0 :(得分:0)

您无法在编译时真正定义变量名称。 最简单的解决方案是将所有这些视图存储到数组或类似的东西中。这是一个例子:

class Component{
    TextView mt;
    TextView tid;
    ImageView img;
    public Component(TextView mt, TextView tid, ImageView img){
        this.mt = mt;
        this.tid = tid;
        this.id = id;
    }
}

.....
Component[] components = new Component[7];
for(int i = 0; i < components.length; i++){
   TextView mt = (TextView) v.findViewById(getResources().getIdentifier("mt" + (i+1), "id", getPackageName()));
   TextView tid = (TextView) v.findViewById(getResources().getIdentifier("tid" + (i+1), "id", getPackageName()));
   ImageView img = (ImageView) v.findViewById(getResources().getIdentifier("imageView" + (i+1), "id", getPackageName()));
   components[i] = new Component(mt, tid, img); 
}
....

要访问那些你可以使用这样的东西

Component c2 = components[3];
c2.mt.setText(......);

答案 1 :(得分:0)

1.init TextViewImageView数组

2.add for loop

3.get id of the view

代码中的

4.findViewById

试试这个。

TextView[]  textViews1 = new TextView[7];
TextView[]  textViews2 = new TextView[7];
ImageView[]  imageViews = new ImageView[7];

for (int j = 0; j < 7; j++) {
    String viewmt = "mt" + (i + 1);
    String viewtid = "tid" + (i + 1);
    String viewImage = "imageView" + (i + 1);
    int resIDmt = getResources().getIdentifier(viewmt, "id", getPackageName());
    int resIDtid = getResources().getIdentifier(viewtid, "id", getPackageName());
    int resIDImage = getResources().getIdentifier(viewImage, "id", getPackageName());

    textViews1[j] = ((TextView) v.findViewById(resIDmt));
    textViews2[j] = ((TextView) v.findViewById(resIDtid));
    imageViews[j] = ((ImageView) v.findViewById(resIDImage));
}

答案 2 :(得分:0)

如果TextView在XML布局中以正确的顺序列出,您可以执行类似的操作,假设它们包含在RelativeLayout中,但它应该对其他布局类的工作方式相同。

  RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.relLayout1);
  ArrayList<TextView> mTextViews = new ArrayList<>();

  for (int i = 0; i < mLayout.getChildCount(); i++) {
      View view = mLayout.getChildAt(i);
      if (view instanceof TextView)
            mTextViews.add((TextView)view);
  }

稍后获取一个TexView myTextView = mTextViews.get[index];