2011-08-13 2 views
2

Android開発には新機能が追加されました。今すぐonCreate()メソッド内のコードはかなり基本的です。すべてのアクティビティボタンでsetOnClickListener()を簡単に行う方法

Button b1 = (Button) findViewById(R.id.button1); 
    Button b2 = (Button) findViewById(R.id.button2); 
    Button b3 = (Button) findViewById(R.id.button3); 
    Button b4 = (Button) findViewById(R.id.button4); 
    Button b5 = (Button) findViewById(R.id.button5); 
    Button b6 = (Button) findViewById(R.id.button6); 
    Button b7 = (Button) findViewById(R.id.button7); 
    Button b8 = (Button) findViewById(R.id.button8); 
    Button b9 = (Button) findViewById(R.id.button9); 
    Button b10 = (Button) findViewById(R.id.button10); 
    Button b11 = (Button) findViewById(R.id.button11); 
    Button b12 = (Button) findViewById(R.id.button12); 
    Button b13 = (Button) findViewById(R.id.button13); 
    Button b14 = (Button) findViewById(R.id.button14); 
    Button b15 = (Button) findViewById(R.id.button15); 
    Button sqrt = (Button) findViewById(R.id.button16); 
    Button b17 = (Button) findViewById(R.id.button17); 
    Button b18 = (Button) findViewById(R.id.button18); 
    Button b19 = (Button) findViewById(R.id.button19); 
    Button b20 = (Button) findViewById(R.id.button20); 
    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    b4.setOnClickListener(this); 
    b5.setOnClickListener(this); 
    b6.setOnClickListener(this); 
    b7.setOnClickListener(this); 
    b8.setOnClickListener(this); 
    b9.setOnClickListener(this); 
    b10.setOnClickListener(this); 
    b11.setOnClickListener(this); 
    b12.setOnClickListener(this); 
    b13.setOnClickListener(this); 
    b14.setOnClickListener(this); 
    b15.setOnClickListener(this); 
    sqrt.setOnClickListener(this); 
    b17.setOnClickListener(this); 
    b18.setOnClickListener(this); 
    b19.setOnClickListener(this); 
    b20.setOnClickListener(this); 

確かにこれより簡単な方法があります。私は単純なforループを試みましたが、ボタンのid値は 'i'の増分に適合しません。この方法を可能にするためにid値を変更する必要がありますか、それとも簡単な方法がありますか?それは、関連するなら

、私は、Eclipse用のプラグインをAndroidのSDKの最新バージョンを使用していますし、ターゲットのバージョン番号は2.3.3

+1

各ボタンのandroid:onClickを指定してください。 –

答えて

13

あなたのためだけの楽しいトリック、すべてのボタンが同じリスナーを使用しているので、あなたがより少ないと、このような何かを行うことができますコード(それはしかし顕著である可能性が高いではない、あまり効率的ですが):

ViewGroup group = (ViewGroup)findViewById(R.id.myrootlayout); 
View v; 
for(int i = 0; i < group.getChildCount(); i++) { 
    v = group.getChildAt(i); 
    if(v instanceof Button) v.setOnClickListener(this) 
} 
+0

thats道があるべきである(盛り上がる) –

+1

シンプルで美しい。 – DallaRosa

+0

いいえ、私は今まで "instanceof"というキーワードを聞いたことがありませんでした。 – Emiam

1

ボタンの配列を作成し、ループの中でそれを行うです:

少し繰り返し
int[] ids = { R.id.button1, R.id.button2 , ........... }; 
Button[] buttons = new Buttons[ids.length]; 

for (int i = 0; i < ids.length; ++i) { 
    buttons[i] = (Button)findViewByIf(ids[i]); 
    buttons[i].setOnClickListener(this); 
} 
+0

パーフェクト、ありがとう – Shane

+0

あなたが作成した 'ids'配列と' findViewById'を使って参照します。 – MByD

+0

これはあなたと同じですが、配列とループでのみです。 – MByD

0

何かは次のようになります。

int[] ids = {R.id.button1, R.id.button2, ... }; 
for (int id:ids) { 
    Button b = (Button) findViewById(id); 
    b.setOnClickListener(this); 
} 
8

onCreateでコールがルートレイアウト

ViewGroup rootLayout=(ViewGroup) findViewById(R.id.root_layout); 

を得るための深い検索に再帰を使用して(このメソッドに渡しますまた、あなたリットルのXMLファイルでこれを設定することができ、ボタン)

public void setAllButtonListener(ViewGroup viewGroup) { 

    View v; 
    for (int i = 0; i < viewGroup.getChildCount(); i++) { 
     v = viewGroup.getChildAt(i); 
     if (v instanceof ViewGroup) { 
      setAllButtonListener((ViewGroup) v); 
     } else if (v instanceof Button) { 
      ((Button) v).setOnClickListener(myButtonsListener); 
     } 
    } 
} 

幸運

+0

再帰的に検索するのを忘れてしまった。素敵な追加! – kcoppock

+1

任意のレベルの再帰を処理するため、これが最適なソリューションです。 –

関連する問題