Easy way to setOnClickListener() on all Activity Buttons

后端 未结 4 1940
迷失自我
迷失自我 2021-02-06 19:07

New to android development, but not entirely new to Java. Right now my code inside the onCreate() method is quite basic:

    Button b1 = (Button) findViewById(R.         


        
4条回答
  •  Happy的楠姐
    2021-02-06 19:53

    Something a little less repetitive could be:

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

提交回复
热议问题