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