Button, ImageButton event Listener (Setonclicklistener)
Method One: through anonymous inner class implementation
The code is as follows:
PackageCom.note.demo2;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsactionbaractivity {//Declare login button Loginbutton PrivateButton Loginbutton; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //initializes the button, assigns the button in the XML to the declared variable, and searches through the ID of R. //Findviewbyid Returns a View object that is to be converted to a button object and set to a button listener object. loginbutton=(Button) Findviewbyid (R.id.button1); //1. The Listener event is implemented by anonymous inner classLoginbutton.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//Listen for the Click button action in the current OnClick methodShow ("Through the inner class implementation, you clicked on Me Oh"); } });
} Public voidShow (String str) {Toast toasts=toast.maketext ( This, str, toast.length_short); Toasts.show (); } Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) { intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
Results:
Method Two: Implement by independent class
PackageCom.note.demo2;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {//Declare login button Loginbutton PrivateButton Loginbutton; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //initializes the button, assigns the button in the XML to the declared variable, and searches through the ID of R. //Findviewbyid Returns a View object that is to be converted to a button object and set to a button listener object. loginbutton=(Button) Findviewbyid (R.id.button1); //2, monitoring events through the independent class implementationLoginbutton.setonclicklistener (listener); } Onclicklistener Listener=NewOnclicklistener () { Public voidOnClick (View v) {Show ("By independent class implementation, you clicked on Me Oh"); } }; Public voidShow (String str) {Toast toasts=toast.maketext ( This, str, toast.length_short); Toasts.setgravity (Gravity.top,0, 220); Toasts.show (); } Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
The results are as follows:
Method Three: Realize event monitoring by means of interface
PackageCom.note.demo2;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActionbaractivityImplementsOnclicklistener {//Declare login button Loginbutton PrivateButton Loginbutton; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //initializes the button, assigns the button in the XML to the declared variable, and searches through the ID of R. //Findviewbyid Returns a View object that is to be converted to a button object and set to a button listener object. loginbutton=(Button) Findviewbyid (R.id.button1); //implementing an interface-based approach to monitoring eventsLoginbutton.setonclicklistener ( This); } Public voidOnClick (View v) {Show ("By implementing the method of the interface"); } Public voidShow (String str) {Toast toasts=toast.maketext ( This, str, toast.length_short); Toasts.setgravity (Gravity.top,0, 220); Toasts.show (); } Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); } }
The results are as follows:
Android Event Monitor (ii)--click the mouse event