2011-08-13 37 views
2

對於開發人員來說是新手,但對於Java來說並不是全新的。現在我的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值才能使此方法可行,還是有更簡單的方法?

如果是相關的,我使用的和最新版本的Android SDK的,Eclipse插件目標版本號是2.3.3

+1

你也可以爲你的l設置這在xml文件ayout通過爲每個按鈕指定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

多數民衆贊成的方式應該(重拳出擊) –

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

然後將它傳遞給這個方法,(使用遞歸的深搜索按鈕)

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

這是最好的解決方案,因爲它可以處理任何級別的遞歸。 –

相關問題