[关闭]
@Yano 2016-01-03T08:53:41.000000Z 字数 757 阅读 1218

Android Button click 事件监听

Android


Activity 实现 android.view.View.OnClickListener。

  1. public class MainActivity extends Activity implements
  2. android.view.View.OnClickListener {
  3. private Button button1, button2, button3;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. button1 = (Button) findViewById(R.id.button1);
  9. button2 = (Button) findViewById(R.id.button2);
  10. button3 = (Button) findViewById(R.id.button3);
  11. button1.setOnClickListener(this);
  12. button2.setOnClickListener(this);
  13. button3.setOnClickListener(this);
  14. }
  15. public void onClick(View v) {
  16. switch (v.getId()) {
  17. case R.id.button1:
  18. System.out.println("button1");
  19. break;
  20. case R.id.button2:
  21. System.out.println("button2");
  22. break;
  23. case R.id.button3:
  24. System.out.println("button3");
  25. break;
  26. }
  27. }
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注