2

動作中のアプリケーションがありますが、コードを最適化したいと考えています。以下は、10 個の個別のイメージボタンを作成し (それぞれのオブジェクト名と XML 参照が増加していることに注意してください)、それらのリスナーを設定する 1 つの例です。おそらく動的メソッド/ループで、これを行うためのより最適な方法を提案できますか? ありがとう....

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

5 に答える 5

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);
}
于 2013-04-25T11:55:47.853 に答える
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);
}

ただし、通常の方法と比較すると非常に遅いです。

于 2013-04-25T12:08:08.410 に答える
0

を設定するだけの場合は、メソッドonClickListener全体を取り除き、レイアウト xmlinitialiseButtons()に追加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
    }
}
于 2013-04-25T12:38:23.597 に答える
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 のリストを入れることもできます。

于 2013-04-25T12:02:25.670 に答える