Chinese
The adobe®flex™ application is event-driven. The event notifies the programmer when the user interacts with the interface component,
Events also inform programmers when a component's appearance or lifecycle changes significantly, such as creating or destroying a container, or changing the size of a container.
When an instance of a component assigns an event, the Listener object that you register for that event is notified. You can define event listeners (or event handlers) in ActionScript to handle events. You can register an event listener for a component in the MXML declaration, or you can implement the same functionality in ActionScript.
There are three ways to receive notification of an event:
registers an event handler in the MXML
creates an inline (inline) event handler in the MXML
registers an event handler with ActionScript
Registering an event handler in MXML
The first, and most widely used, way to receive event notifications is to define an event handler in MXML that is invoked when the event occurs.
In this example, you define an event handler for the Click event of a Button control. When the user clicks the Button control, the event handler sets the Label control's Text property to "hello,world!".
Example
xmlns:mx= http://www.adobe.com /2006/mxml
width= " height="
horizontalalign= "center" verticalalign= "Middle"
Viewsourceurl= "src/handlingeventseventhandler/index.html"
import flash.events.MouseEvent;
private function clickhandler ( event:mouseevent ): void
{
mylabel.text = "hello,world!";
}
]]>
title= my application horizontalalign= "center"
Paddingtop= "ten" paddingbottom= "ten" paddingleft= "ten"
paddingright= "10"
Fontsize= "/> "
click= "ClickHandler (event);" />&NBSP
Run result diagram:
As the SWF file cannot be embedded in the document, the code runs the results to see the appropriate section in the original.
Create an inline (inline) event handler in MXML
Sometimes the easiest way to respond to an event is to define the event handler entirely in the MXML declaration of a component. This is the use of an inline (inline) event handler.
In the following example, you set the Click property of the
Tip: Using an inline event processor may be faster and less code, but it can also affect the readability, maintainability, and scalability of your code. A good rule of thumb is not to include a statement or ActionScript in an inline event handler. If you must include more complex logic, put it in a ActionScript helper method (the first method) or a ActionScript event handler (that is, the third method).
Example
xmlns:mx= "Http://www.adobe.com/2006/mxml"
Viewsourceurl= "Src/handlingeventsinlinemethod/index.html"
horizontalalign= "center" verticalalign= "Middle"
Width= "height=" >
Title= "My Application" horizontalalign= "center"
paddingtop= "paddingbottom=" paddingleft= "10"
Paddingright= "Ten" >
Fontsize= "/>"
Registering an event listener with ActionScript
You can also respond to events by registering an event handler with ActionScript. In this example, the addEventHandler () method in ActionScript is used to register an event supervisor
Listening device. This addEventHandler () method is placed in the application container for processing
The event handler for the Creationcomplete event.
Tip: When the program starts running, the application Creationcomplete event occurs after the application's appearance and the initialization of its subassembly is complete. The processor of the Creationcomplete event provides a convenient place to place the ActionScript code of the registered event listener.
Example
xmlns:mx= "Http://www.adobe.com/2006/mxml"
Viewsourceurl= "Src/handlingeventsactionscript/index.html"
horizontalalign= "center" verticalalign= "Middle"
Width= "height=" "200"
Creationcomplete= "Creationcompletehandler (event);" >
! [cdata[
Import flash.events.MouseEvent;
Import mx.events.FlexEvent;
Private function
Creationcompletehandler (event:flexevent): void
{
ClickHandler);
}
Listen for the "click event on" Button control
Mybutton.addeventlistener (Mouseevent.click,
Private Function ClickHandler (event:event): void
{
MyLabel.Text = "Hello, world!";
}
]]>
Title= "My Application" horizontalalign= "center"
paddingtop= "paddingbottom=" paddingleft= "10"
Paddingright= "Ten" >
Fontsize= "/>"
Run result diagram:
Small Editor: This article quotes Dreamer ' s Blog and hereby thank the author for his efforts. If you think this tutorial is just what you need, please don't forget to go to the author's blog to cheer.