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