Archive for June, 2009

Android – Events the easy (AS3) way

Tuesday, June 9th, 2009

I like the EventDispatcher class from ActionScript 3 so I decided to write one for Java.
Usage

1
2
3
4
5
6
7
foo.addEventListener("MyEvent", new EventListener() {
    public void run(Event event) {
        // remove the listener if no longer needed
        event.target.removeEventlistener(event.type, this);
        // do stuff
    }
}

Now your class can either extend EventDispatcherImp or implement EventDispatcher and you’re ready to dispatch events.

Via inheritance

1
2
3
import com.wumedia.events.EventDispatcher.EventDispatcherImpl;
public class Dispatcher extends EventDispatcherImpl {
}

Via composition

1
2
3
4
5
6
import com.wumedia.events.EventDispatcher.EventDispatcherImpl;
public class Dispatcher implements EventDispatcher {
    public Dispatcher() {
        _dispatcher = new EventDispatcherImpl(this);
    }
}

(more…)