gpt4 book ai didi

android - 我如何找出哪个 TextView 连接到哪个数组?

转载 作者:行者123 更新时间:2023-11-29 21:11:12 25 4
gpt4 key购买 nike

我想在 for 循环中设置 TextView 的背景。但是因为您不能使用数组作为 TextView 的名称,所以我不知道该怎么做。

findViewById(R.id.array[x]).setBackgroundColor(Color.parseColor("#ffb6c1"));

我的 TextView 被称为:array1、array2、array3...

我想用数字代替 [x]。像这样:

for (int x=1;x<13;x++){
findViewById(R.id.array[x]).setBackgroundColor(Color.parseColor("#ffb6c1"));
}

我该怎么做?

最佳答案

你可以尝试如下...

Resources res = getResources();

for (int x = 1; x < 13; x++){

int id = res.getIdentifier("array" + x, "id", getContext().getPackageName());
findViewById(id).setBackgroundColor(Color.parseColor("#ffb6c1"));
}

关于android - 我如何找出哪个 TextView 连接到哪个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22767473/

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