V Notation for button events:
1. Anonymous inner class
2, class implementation View.onclicklistener interface
3. Creating an instantiated interface object
4. Use inner class
5. Custom methods, configuring Android:onclick Properties
Import Android.media.jetplayer.onjeteventlistener;import Android.os.bundle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.app.dialog;import Android.content.dialoginterface;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imagebutton;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener{button Button1,button2, Button4,button5;imagebutton button3; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (New Onclicklistener () {///The First way: button click event implemented within anonymous internal @overridepublic void OnClick (View arg0) {// TODO auto-generated Method Stubtoast.maketext (mainactivity.this, "Anonymous internal implementation of button click events", Toast.length_short). Show ();}); Button2 = (Button) Findviewbyid (R.id.buttoN2); Button2.setonclicklistener (this);//The third type: Create an instantiated interface object Button3 = (ImageButton) Findviewbyid (r.id.button3); O Nclicklistener listener = new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stu Btoast.maketext (Mainactivity.this, "Create instantiated Interface Object", Toast.length_short). Show ();}}; Button3.setonclicklistener (listener);//Fourth method: Use inner class button4 = (Button) Findviewbyid (R.ID.BUTTON4); Button4.setonclicklistener (New Myonclicklistener ());} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} The second way: Implement the View.onclicklistener interface to implement the OnClick () method in the interface @overridepublic void onclick (View arg0) {//TODO auto-generated Method Stubswitch (Arg0.getid ()) {case R.id.button2:toast.maketext (this, "implement the OnClick () methods in the View.onclicklistener interface implementation interface ", Toast.length_short). Show (); break;}} Class Myonclicklistener implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODOAuto-generated method Stubtoast.maketext (Mainactivity.this, "Way of the Inner class", Toast.length_short). Show ();}} Fifth way: Custom method: Configure the Android:onclick property public void Onbutton (view view) {switch (View.getid ()) {case R.ID.BUTTON5: Toast.maketext (This, "custom method, configure Android:onclick Properties", Toast.length_short). Show (); break;}} public void onButton7 (view view) {Toast.maketext (this, "aaaaaaaaaaaaaaaaaaaaa", Toast.length_short). Show (); Setcontentview (r.layout.activity_main2);}}
V Summary of Android button events