标签:android blog http java ar for 2014 log on
1、工程目录
2、FireEvent.java
package com.example.fireeevent; import java.util.EventObject; //着火事件类,绑定事件源 public class FireEvent extends EventObject { private Object eventSource; public FireEvent(Object source) { super(source); // TODO Auto-generated constructor stub this.eventSource = source; } public Object getEventSource() { return eventSource; } }
3、Listener.java
package com.example.fireeevent; import java.util.EventListener; //着火监听器,监听着火事件 public interface Listener extends EventListener { public void listen(FireEvent e); }
package com.example.fireeevent; public class FireListener implements Listener { public void listen(FireEvent e) { // TODO Auto-generated method stub System.out.println("房子着火啦!!!"); } }
5、House.java
package com.example.fireeevent; import java.util.ArrayList; import java.util.List; //事件源房子类 public class House { private boolean flag = false; private List listeners = new ArrayList(); public 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 FireEvent(this)); } } }
package com.example.fireeevent; //测试类 public class Test { public static void main(String[] args) { House h = new House(); h.addListener(new FireListener()); h.addListener(new Listener() { public void listen(FireEvent e) { // TODO Auto-generated method stub System.out.println("冒烟啦!!!"); } }); h.setFlag(true); } }
事件处理机制:
(1)event object:就是事件产生时具体的“事件”,用于listener的相应方法之中,作为参数,一般存在于listener的方法之中。
(2)event source:具体接受事件的实体,比如说,你单击一个button,那么button就是event source。
(3)event listener:事件监听器,当有其对应的event object产生的时候,它就调用相应的方法进行处理。这种方式也称为方法回调(callback)
android开发详解(六)--------------事件处理机制
标签:android blog http java ar for 2014 log on
原文地址:http://blog.csdn.net/hulan_baby/article/details/39402943