gpt4 book ai didi

java - for循环中的Findviewbyid()

转载 作者:行者123 更新时间:2023-11-30 10:23:28 25 4
gpt4 key购买 nike

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

最佳答案

1.初始化TextViewImageView数组

2.添加for循环

3.获取 View 的id

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

关于java - for循环中的Findviewbyid(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com