Android Button Event handling

Source: Internet
Author: User

Generally only need to handle the button's click event, but to let a button handle multiple events, you have to listen to multiple methods at the same time.

Onclicklistener Click events

Onlongclicklistener Long Press Events

Ontouchlistener Touch Events

A colleague listens to three events, and only Ontouchlistener is triggered.

Package Demo.button;import android.app.activity;import android.os.bundle;import android.view.ContextMenu; Import Android.view.contextmenu.contextmenuinfo;import Android.view.motionevent;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.view.oncreatecontextmenulistener;import Android.view.view.onlongclicklistener;import Android.view.view.ontouchlistener;import Android.widget.Button; Import Android.widget.Toast; Public classtestbuttonactivity extends activity{Button test=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                        Setcontentview (R.layout.main); Test=(Button) Findviewbyid (r.id.test); Test.setonclicklistener (NewMyonclicklistener ()); Test.setonlongclicklistener (NewMyonlongclicklistener ()); Test.setontouchlistener (NewMyontouchlistener ()); }            classMyonclicklistener implements Onclicklistener {@Override Public voidOnClick (View v) {toast.maketext (Getapplicationcontext (),"Click events", Toast.length_short). Show (); }        }       classMyonlongclicklistener implements Onlongclicklistener {@Override Publicboolean Onlongclick (View v) {toast.maketext (Getapplicationcontext (),"Long Press Events", Toast.length_short). Show ();return true;} }      classMyontouchlistener implements Ontouchlistener {@Override PublicBoolean OnTouch (View V, motioneventEvent){if(Event. getaction () = =Motionevent.action_down) {Toast.maketext (Getapplicationcontext (),"Press the button event", Toast.length_short). Show ();} if(Event. getaction () = =motionevent.action_up) {Toast.maketext (Getapplicationcontext (),"pop up button event", Toast.length_short). Show ();} //Returns true to indicate that the event is processed and interrupts the system's handling of the event. False the system handles the corresponding event at the same timereturn true;} }}

Android Button Event handling

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.