Java 在所有活动按钮上设置setOnClickListener()的简单方法

Java 在所有活动按钮上设置setOnClickListener()的简单方法,java,android,Java,Android,android开发的新手,但对Java并不完全陌生。现在,我在onCreate()方法中的代码非常基本: 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(

android开发的新手,但对Java并不完全陌生。现在,我在onCreate()方法中的代码非常基本:

    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,创建一个按钮数组并循环执行:

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);
}
int[]id={R.id.button1,R.id.button2,…};
按钮[]按钮=新按钮[ID.length];
对于(int i=0;i
重复性稍低的可能是:

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

这对您来说只是一个有趣的技巧,因为您的所有按钮都使用同一个侦听器,所以您可以用更少的代码执行类似的操作(虽然效率较低,但不太可能引人注目):

ViewGroup group=(ViewGroup)findviewbyd(R.id.myrootlayout);
观点五;
对于(int i=0;i
onCreate
中调用获取根布局

ViewGroup rootLayout=(ViewGroup) findViewById(R.id.root_layout);
然后将其传递给此方法(使用递归深入搜索按钮)

public void setAllButtonListener(视图组视图组){
观点五;
对于(int i=0;i

祝你好运

你使用你创建的
ids
数组引用它,然后
findViewById
这和你做的一样,只使用数组和循环。那应该是(砰的一声)酷,我从来没有听说过“instanceof”关键字直到现在=)从来没有想过这种方式很好。您也可以在xml文件中为布局设置此选项,方法是为每个按钮指定android:onClick;很好的补充!这是最好的解决方案,因为它可以处理任何级别的递归。
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);
        }
    }
}