2013-04-25 58 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 
    } 
} 
相关问题