[Html]
Define your own Event class: MyEvent
Package com
{
Import flash. events. Event;
Import flash. events. EventDispatcher;
Public class MyEvent extends Event
{
Public static const EVENT_TEST: String = "EventTest ";
Public var data: Object;
Public static const dis: EventDispatcher = new EventDispatcher ();
Public function MyEvent (type: String, bubbles: Boolean = false, canceable: Boolean = false, data: Object = null)
{
Super (type, bubbles, canceable );
This. data = data;
}
}
}
[Html]
Test page:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml"
Layout = "absolute"
Xmlns: test = "com. *">
<Mx: Script>
<! [CDATA [
Import mx. controls. Alert;
Import com. MyEvent;
/* Event execution process:
* 1. Add a listener for the EventDispatcher of the Custom Event class
* 2. instantiate a Custom Event
* 3. assign a value to the event data (used to pass the value to the listener)
* 4. Listen to the object www.2cto.com returned by processing the event
* The value of MyEvent. EVENT_TEST set here is
* A listener event type flag is provided because
* We will use a custom event class to pass multiple values.
* When different types of values are identified, the event type is used.
*.
*/
Private function test (): void {
// Add a listener for the event
MyEvent. dis. addEventListener (MyEvent. EVENT_TEST, onEnd );
SetValue ();
}
Private function setValue (): void {
// Instantiate an event and assign a value to data
Var e: MyEvent = new MyEvent (MyEvent. EVENT_TEST, false, false, 'dada ');
// Dispatch the event
// Note that you need to use your own dispatcher, instead of using the built-in dispatch method dispatch ();
MyEvent. dis. dispatchEvent (e );
}
Private function onEnd (e: MyEvent): void {
// Process the value returned by the event
Alert. show (e. data. toString ());
}
]>
</Mx: Script>
<Mx: Button label = "Test" id = "btn" click = "test ()"/>
</Mx: Application>
Author: tengdazhang770960436