Flex custom events and pass values

Source: Internet
Author: User

[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

Related Article

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.