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