How click events are implemented in Android

Source: Internet
Author: User

In the previous blog post multiple use of Click event Processing implementation, a friend asked, found a lot of button click Implementation, but there are many blog used in the implementation of the way there are not the same, in the end is how. Today we'll summarize how click events are implemented.

The implementation of Click events is broadly divided into the following three types:

( 1 ) Activity Implement interface mode to implement click events (often used)

(2) Custom method, using configuration file Android:onclick

(3) Implement using Inner class method

( 4 use anonymous inner class implementations to introduce the following types of click events:

Here's a quick example of how the next few click events are implemented by code:(1)Activity implements interface mode for click events
1 /**2 * Activity Implementation Interface mode realization Click event3 * Activity Implementation View.onclicklistener Implementation OnClick (view view) {} method4 * Registering events in the Activity's OnCreate method5  */6  Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener {7     PrivateButton btn;8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); OneBTN =(Button) Findviewbyid (R.id.button); ABtn.setonclicklistener ( This); -     } -      Public voidOnClick (View v) { theToast.maketext (mainactivity. This, "Implementing Interface Mode", Toast.length_long). Show (); -     } -}
(2) Custom method, use layout config file Android:onclick
1 /**2 * Use configuration file to implement click events3 * Use the OnClick property in the configuration file in layout to specify the processing method when the event is triggered,4 * Provide a method with the same name in Activity format for public void XXX (View v) {...}5  */6  Public classMainactivityextendsappcompatactivity {7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten     } One      Public voidClick (View v) { AToast.maketext (mainactivity. This, "Custom", Toast.length_long). Show (); -     } -}
(3) Implement using Inner class method
1 /**2 * Click events are implemented using an internal class approach3 * Define a View.onclicklistener implementation class to implement the OnClick method. 4 * Registering events in the Activity's OnCreate method5  */6  Public classMainactivityextendsappcompatactivity {7     PrivateButton btn;8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); OneBTN =(Button) Findviewbyid (r.id.button2); ABtn.setonclicklistener (NewListener ()); -     } -     classListenerImplementsview.onclicklistener{ the @Override -          Public voidOnClick (View v) { -Toast.maketext (mainactivity. This, "Inner class", Toast.length_long). Show (); -         } +     } -}
(4)using an anonymous inner class implementation
1 /**2 * Click events are implemented using anonymous inner class mode3 * Register button click event when using anonymous inner class directly4  */5  Public classMainactivityextendsappcompatactivity {6     PrivateButton btn;7     protected voidonCreate (Bundle savedinstancestate) {8         Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);TenBTN =(Button) Findviewbyid (r.id.button3); OneBtn.setonclicklistener (NewView.onclicklistener () { A @Override -              Public voidOnClick (View v) { -Toast.maketext (mainactivity. This, "Anonymous inner class", Toast.length_long). Show (); the             } -         }); -     } -}
Add:

Different components to implement different types of click events, such as Onitemclicklistener,oncheckedchangelistener,onratingbarchangelistener, Onmultichoiceclicklistener,ondate/timesetlistener,onscrolllistener, Onchildclicklistener,setontouchlistener,onpagechangelistener, Onmenuitemclicklistener, Oneditoractionlistener,Oneditoractionlistener and so on various click event Processing form.

How click events are implemented in Android

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.