【问题标题】:How do i find out which TextView is connected to which array?我如何找出哪个 TextView 连接到哪个数组?
【发布时间】:2014-03-31 16:48:17
【问题描述】:

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

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

我该怎么做?

【问题讨论】:

  • 您是否将TextView id 保存在数组中?
  • 不,我在数组中有字符串 (array[1],array[2],array[3]...) 这些数组显示在 TextViews (array1,array2,array3. ..) 我想自动将连接的数组分配给正确的数组。 array[1] = array1 array[2] = array2 但是应该自动添加 Textviews (array1,array2,array3...) 的数字。 array[1] 应改为 array1 等。
  • 那些TextViews是否存在于XML中?
  • 是的,我只想改变他们的背景

标签: android arrays for-loop textview


【解决方案1】:

你可以尝试如下...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    相关资源
    最近更新 更多