Android Event Monitor (ii)--click the mouse event

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.