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

标签 android arrays for-loop textview

我想在 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/

相关文章:

python - [for 'elem' in myList] 与 [for 'i' in range(x, y)] 的区别是什么,引用列表元素

matlab - 如何在 Matlab 中向量化这个 for 循环?

android fragment View 模型初始化失败

java - 如何从注册号牌中获取车主信息?

java - Android 的 SQLiteOpenHelper 测试类

javascript - 数组排序的最佳实践,PHP 还是 Javascript?

python - While 循环列表?

android - 错误 : cause android. compileSdkVersion 丢失

c - 按字母顺序对 C 中的字符指针数组进行排序时出现段错误

javascript - 在 LazyJS 中的哪个位置