Follow me on Android's four-event-driven model

Source: Internet
Author: User

The Android event-driven model requires deep learning and understanding, and the three elements of the event-driven model are as follows:

Event-driven models

Event Source : The producer of the event, such as: button

Typically has the ability to register and cancel listeners

Listener : The recipient of an event, usually the object of a class you write yourself

A class that implements the event interface supported by the event source

Event : A specific event generated by an event source

An event source can produce multiple events

A listener can receive multiple events

Handlers for events typically reside inside the listener

Event-driven models
Work steps

1, define the listener, write a processing method for each event
2. Register the Listener object with the event source
3. When an event source occurs, the corresponding method in the listener is called to complete the event processing


Inner class form : An inner class defines another class within a class, using an inner class to define the event listener class

Class BtnListener1 implements view.onclicklistener{@Overridepublic void OnClick (View arg0) {tv1.settext ("You hit me!");}

listeners typically use Java anonymous classes to implement
The definition of an anonymous class occurs at the same time as object creation
The specific definition format is as follows:


Use activity itself as listener class
Cons: This form may clay into a procedural structure disorder

public class Mainactivity extends Activity implements view.onclicklistener{...}
to populate the control with events:
1: Determine the event source, such as a button
2: Clear the event to listen to, Android is usually a listener handling an event

3: Writing listeners through anonymous classes, registering listeners at the same time

Locate the event source object Button btn = (button) Findviewbyid (R.ID.OKBTN);//Register Listener Btn.setonclicklistener (new Onclicklistener () {        @ Override public        void OnClick (View v) {                log.d ("Tip", "button clicked");}        );


Follow me on Android's four-event-driven model

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.