2013-09-21 27 views
0

我對Android開發相當新,並且正在嘗試創建一個for循環來檢查共享首選項文件中的每個鍵並將圖像按鈕設置爲可見到每個鍵的值。如果在共享首選項中存在值圖像按鈕(Android)

例子:

ImageButton button101 = (ImageButton) findViewByID(R.id.button101); 

如果值101是任何在我的共享偏好文件的關鍵,我需要button101設置爲可見。我卡住的地方是我似乎無法弄清楚如何根據這個值引用button101。我想這樣的事情沒有成功:

String.valueOf("button" + sharedPrefs.getInt("key", 0)).setVisibility(View.VISIBLE); 

回答

0

獲取的參考圖像,然後再將其設置爲可見或不可見(我認爲你只是在做出來的東西順序):

String key = String.valueOf("button" + sharedPrefs.getInt("key", 0)); 
ImageButton button101 = (ImageButton) findViewByID(key); 
button101.setVisibility(View.VISIBLE); 
+0

我認爲'findViewById'只接受一個'int'匹配視圖的資源ID。爲了通過他們的標籤或文本搜索視圖,您需要取消引用其父視圖。 – Jon

+0

謝謝,我根據你的建議進行了調整,必須改變才能找到ViewWithTag,但它現在工作得很完美。 – Trent

+0

太好了。真高興你做到了。有時Android可能很具挑戰性,所以堅持下去。 – Booger

0

嘗試:

String key = String.valueOf("button" + sharedPrefs.getInt("key", 0)); 
ImageButton button101 = (ImageButton) findViewById(R.id.key); 
button101.setVisibility(View.VISIBLE);