2013-04-25 62 views
2

我有一個工作的應用程序,但想優化代碼。下面是一個例子,我創建了10個單獨的圖像按鈕(注意每個對象的名稱和XML引用都是遞增的)並設置它們的監聽器。任何人都可以提出一個更優化的方式來做到這一點,也許在一個動態的方法/循環請嗎?謝謝....優化Android/Java代碼 - 創建同一對象類型的多個實例

private void initialiseButtons() { 
    ImageButton imageButton1 = (ImageButton)this.findViewById(R.id.imageButton1); 
    imageButton1.setOnClickListener(this); 
    ImageButton imageButton2 = (ImageButton)this.findViewById(R.id.imageButton2); 
    imageButton2.setOnClickListener(this); 
    ImageButton imageButton3 = (ImageButton)this.findViewById(R.id.imageButton3); 
    imageButton3.setOnClickListener(this); 
    ImageButton imageButton4 = (ImageButton)this.findViewById(R.id.imageButton4); 
    imageButton4.setOnClickListener(this); 
    ImageButton imageButton5 = (ImageButton)this.findViewById(R.id.imageButton5); 
    imageButton5.setOnClickListener(this); 
    ImageButton imageButton6 = (ImageButton)this.findViewById(R.id.imageButton6); 
    imageButton6.setOnClickListener(this); 
    ImageButton imageButton7 = (ImageButton)this.findViewById(R.id.imageButton7); 
    imageButton7.setOnClickListener(this); 
    ImageButton imageButton8 = (ImageButton)this.findViewById(R.id.imageButton8); 
    imageButton8.setOnClickListener(this); 
    ImageButton imageButton9 = (ImageButton)this.findViewById(R.id.imageButton9); 
    imageButton9.setOnClickListener(this); 
    ImageButton imageButton0 = (ImageButton)this.findViewById(R.id.imageButton0); 
    imageButton0.setOnClickListener(this); 
} 

回答

0

您可以使用數組,列表...

例如:

private static final int[] BUTTONS_IDS = new int[] {R.id.imageButton0, R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4, R.id.imageButton5, R.id.imageButton6, R.id.imageButton7, R.id.imageButton8, R.id.imageButton9}; 

ImageButton[] buttons = new ImageButton[BUTTON_IDS.length]; 

private void initialiseButtons() { 
    for (int i = 0; i < BUTTONS_IDS.length; i++) { 
     buttons[i] = (ImageButton) findViewById(BUTTONS_IDS[i]); 
     buttons[i].setOnClickListener(this); 
    } 
} 

你也可以把在arrays.xml ID列表文件。

+0

謝謝你,我實現了這一點,它完美的作品。如果其他人複製/粘貼,只需進行一次更正 - 在前兩行中將BUTTON_IDS更改爲BUTTONS_IDS,以便始終保持一致的變量名稱。這就是說,它仍然是最高迴應njzk2。 – 2013-04-25 14:01:57

+0

感謝您的錯字更正,我編輯它。 – njzk2 2013-04-26 07:43:18

1

您可以通過使用http://androidannotations.org/減少樣板代碼,這將讓你做些事情一樣,

@ViewById 
ImageButton imageButton1; 

但它會也許是更好使用數組或列表的按鈕,而不是多次引用,類似的東西,例如:

private void init() { 
    int[] ids=new int[]{R.id.imageButton1, R.id.imageButton2 ...}; 
    List<ImageButton> buttons=new ArrayList<ImageButton>; 
    for(int id : ids) { 
     buttons.add((ImageButton)this.findViewById(id)); 
    } 
} 

你就可以輕鬆地重複的列表,例如設置監聽

for(ImageButton button : buttons) { 
    button.setOnClickListener(this); 
} 
0

我不能嘗試它,因爲我沒有Android Environmetn在這裏。但我認爲這應該工作:

private void initialiseButtons() { 
    for(int i = 0; i < 10; i++) { 
     ImageButton imageButton = (ImageButton)this.findViewById(R.id.getClass(). 
     getField("imageButton" + i).get(R.id)); 
     imageButton.setOnClickListener(this); 
    } 
} 
+2

而非R.id .getClass(),有getResources()。則getIdentifier(字符串,字符串,字符串),這是清潔 – njzk2 2013-04-25 12:03:50

1

您可以使用循環並使用getIdentifier()方法。

int idToInitialize; 
ImageButton ib; 
for (int i = 0; i < 9; i++) { 
    idToInitialize = getResources().getIdentifier("imageButton" + i, "id", getPackageName()); 
    ib = (ImageButton) this.findViewById(idToInitialize); 
    ib.setOnClickListener(this); 
} 

Hovewer,如果我們比較正常的方法,它是非常緩慢的。

0

如果它只是用於設置onClickListener,你可以擺脫掉你的整個initialiseButtons()方法,只是在佈局XML添加android:onClick="handleButtonClick"並定義方法在您的活動,像這樣:

public void handleButtonClick(View view) { 
    switch(view.getId()) { 
    case R.id.imageButton1: // handle button 1 pressed .... 
    case R.id.imageButton2: // handle button 2 pressed .... 
    // ... handle further buttons 
    } 
} 
相關問題