1
1 @Override2 protected void onCreate(Bundle savedInstanceState) {3 super.onCreate(savedInstanceState);4 setContentView(R.layout.activity_main);5 }6 7 public void doSomething(View view){8 Log.e("MainActivity","Clicked");9 }
点击button时 call doSomething();
如果有多个Button,则通过下面两种方式做出不同的响应:
1 7 8 14 15
1 public void doSth1(View view){ 2 Log.e("MainActivity", "Clicked1"); 3 } 4 5 public void doSth2(View view){ 6 7 if(view.getId()==R.id.button2) 8 Log.e("MainActivity","Clicked2"); 9 if(view.getId()==R.id.button3)10 Log.e("MainActivity","Clicked3");11 }
如上,在doSth2()中通过view.getId()来获取不同的值,注意@+id/button3和R.id.button3是分别在xml和java中对同一个整数值的描述