Android Development Detailed (vi)--------------event handling mechanism

Source: Internet
Author: User

1. Engineering Catalogue


2, Fireevent.java

Package Com.example.fireeevent;import java.util.eventobject;//Fire event class, binding event source public class FireEvent extends EventObject { Private Object Eventsource;public FireEvent (object source) {super (source);//TODO Auto-generated constructor Stubthis.eventsource = source;} Public Object Geteventsource () {return eventSource;}}

3, Listener.java

Package Com.example.fireeevent;import java.util.eventlistener;//Fire listener, monitoring fire events public Interface Listener extends EventListener {public void Listen (FireEvent e);}

4, Firelistener.java

Package Com.example.fireeevent;public class Firelistener implements Listener {public void Listen (FireEvent e) {//TODO Aut O-generated method StubSystem.out.println ("The House is on fire!!! ");}}


5, House.java

Package Com.example.fireeevent;import Java.util.arraylist;import java.util.list;//Event Source House category public class Home {private Boolean flag = false;private List listeners = new ArrayList ();p ublic boolean getflag () {return flag;} public void Setflag (Boolean flag) {This.flag = FLAG;IF (flag) {ntf ();}} public void AddListener (Listener l) {listeners.add (L);} Public List Getlisteners () {return listeners;} public void ntf () {for (int i = 0; i < listeners.size (); i++) {Listener L = (Listener) listeners.get (i); L.listen (New Fi Reevent (this));}}}

6, Test.java

Package com.example.fireeevent;//Testing class public class Test {public static void main (string[] args) {House H = new House (); H.add Listener (New Firelistener ()); H.addlistener (new Listener () {public void Listen (FireEvent e) {//TODO auto-generated Method StubSystem.out.println ("Smoke!!! ");}}); H.setflag (True);}}


Note: Build a Java project.

Event handling mechanism:

(1) Event object: Is the event when the specific "event", for the corresponding method of listener, as a parameter, generally exist in the listener method.

(2) Event Source: The entity that specifically accepts the event, for example, if you click a button, then the button is the event source.

(3) Event listener: Events Listener, when it has its corresponding event object generated, it calls the appropriate method for processing. This approach is also called method callback (callback)

Android Development Detailed (vi)--------------event handling mechanism

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.