2012-02-05 20 views
1

我有一個創建64個按鈕的循環,內環路,按鈕獲得使用button.setId(n)如何通過其給定ID調用視圖

的問題是,我該如何調用按鈕具有一定的ID ID來改變它的屬性。

理想情況下,我在尋找類似的東西這個

ImageView button2 = (ImageView)findViewById(button.("with id 14, for example"))

回答

1

如果要設置的ID,你知道的ID。因此,您撥打findViewById()並附上您設定的ID。

督察,如果你叫:

button.setId(14); 

您稍後致電:

findViewById(14); 

當然,歡迎你來牽你的Button對象數組或東西,並訪問他們的方式。

+0

好的,謝謝,但有無論如何找到循環外的id? – hazard1994 2012-02-05 14:15:19

+0

@ hazard1994:您是設置ID的人。您需要跟蹤您分配的ID值。 – CommonsWare 2012-02-05 14:28:10

0

一種方式做到這一點:每次創建按鈕時添加其引用到像一個容器對象:

Map<Integer, ImageView> buttonViews = new HashMap<Integer, ImageView>(); 

要添加一個按鈕狀態:

ImageView b = new ImageView (this); 
buttonViews.put(id, b); 

要更改屬性的任何按鈕:

buttonViews.get(id).setImageResource(R.drawable.buttonImage1); 

用這種方法你可以使各種效果...

+0

非常感謝你,我一直堅持這個問題很多年了! – hazard1994 2012-02-05 19:47:12