The events provided by the FLEX system are basically the same as those provided by DOM events. However, unlike traditional web development, custom events can be added to custom components. in a logon box, when the login button is clicked, a custom event is triggered, such as a "login" event. Although this login event is ultimately a click event of a button, the Custom Event is more what are the advantages of an event: first, the event name is user-defined, which can visually represent the event itself. Unlike the previous one, we still need to consider which button to press and so on to facilitate the communication between components. The following example A custom component is testcomponent, this custom component has a button and an input box. When you press this button, a new event is created.
The upload data event is declared as a textevent. The textevent can pass data (the value of the textinput Control) through the text attribute ). Eventtest. the custom component testcomponent is referenced in mxml, the Custom Event metadata data in testcomponent is captured, and the string testcomponent of the textinput control in testcomponent is output. mxml :( we created it under the/src/view folder)
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Mx: vbox xmlns: MX = "http://www.adobe.com/2006/mxml" Height = "300">
- <! -- Declare the event that this component will throw -->
- <Mx: Metadata>
- [Event (name = "Your Data", type = "flash. Events. textevent")]
- <! -- The upload data here is equivalent to click, and the future: Event: textevent is equivalent to E: mouseevent -->
- </MX: Metadata>
- <Mx: SCRIPT>
- <! [CDATA [
- Private function fnonclick_testcomp (): void {
- // The event1 constructor must pass at least one event name.
- VaR event1: textevent = new textevent ("invalid data ");
- Event1.text = mytxtinput. text;
- // Use disptchevent to throw the created event
- Dispatchevent (event1 );
- }
- ]>
- </MX: SCRIPT>
- <Mx: Label text = "this is the test component"/>
- <Mx: textinput id = "mytxtinput"/>
- <! -- Custom events when the button is clicked -->
- <Mx: button label = "clickme" Click = "fnonclick_testcomp ()"/>
- </MX: vbox>
Eventtest. mxml:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical"
- Xmlns: view = "view. *">
- <Mx: SCRIPT>
- <! [CDATA [
- Import MX. Controls. Alert;
- Private function fnon1_data (Event: textevent): void {
- Alert. Show ("event has been use. event. Text =>" + event. Text );
- }
- ]>
- </MX: SCRIPT>
- <! -- Call our custom component and process its upload data event, just like a click event -->
- <View: testcomponent upload data = "fnon1_data (event)"/>
- </MX: Application>
In addition, the following article is also cool:
Http://www.cnblogs.com/xxcainiao/archive/2008/03/08/1096815.html
Compile a flex component from scratch. Flex custom controls.