2014-03-31 32 views
1

我想在for循環中設置TextView的背景。 但是,因爲你不能使用數組作爲TextViews的名稱,我不知道如何做到這一點。如何找出哪個TextView連接到哪個數組?

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

我TextViews被稱爲:數組1,數組2,ARRAY3 ...

,我想放的數字,而不是[X]。 想要這樣:

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

我該怎麼做?

+0

你保存'TextView' ID陣列? –

+0

不,我有數組中的字符串(數組[1],數組[2],數組[3] ...) 這些數組顯示在TextViews(array1,array2,array3 ...) 我會喜歡自動將連接的數組分配給正確的數組。 array [1] = array1 array [2] = array2 但是Textviews(array1,array2,array3 ...)的編號應該自動添加。 array [1]應該改成array1等 – Fynn

+0

是那些'TextViews'存在於XML中? –

回答

2

您可以嘗試如下...

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

它現在的作品:)非常感謝! – Fynn

相關問題