优化Android / Java代码 - 创建相同对象类型的多个实例

时间:2013-04-25 11:51:41

标签: java android xml

我有一个有效的应用程序,但想要优化代码。下面是一个示例,我创建了10个单独的图像按钮(注意每个图像按钮的递增对象名和XML参考)并设置它们的监听器。任何人都可以建议一个更优化的方法,也许在动态方法/循环中吗?感谢....

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);
}

5 个答案:

答案 0 :(得分: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));
    }
}

然后您可以轻松地在List上进行迭代,例如设置侦听器

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

答案 1 :(得分: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,如果我们将其与普通方法进行比较,那就非常慢了。

答案 2 :(得分:0)

我无法尝试,因为我这里没有Android环境。但我认为这应该有效:

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);
   }
}

答案 3 :(得分: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);
    }
}

您还可以将id列表放在arrays.xml文件中。

答案 4 :(得分: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
    }
}
相关问题