———使用MXML触发运行时代码

Flex applications are driven by run-time events, such as when a user selects a Button control. You can specify event listeners, which consist of code for handling run-time events, in the event properties of MXML tags. For example,
the <mx:Button> tag has a click event property in which you can specify ActionScript code that executes when the Button control is clicked at run time. You can specify simple event listener code directly in event properties.
To use more complex code, you can specify the name of an ActionScript function defined in an <mx:Script> tag.
The following example shows an application that contains a Button control and a TextArea control. The click property of the Button control contains a simple event listener that sets the value of the TextArea control’s text property to the text Hello World.
    Flex程序是在运行时由事件驱动的,比如,用户选择了一个button控件。你可以在MXML代码的标签中使用event属性来为其指定某事件的监听器。举例来说,<mx:Button>标签拥有一个click事件属性,你可以为它指定一个简单的由ActionScript编写的处理代码,以便在运行期间按钮被点击时进行相应的事物处理。你可以直接在event属性中编写简单的监听器,如果要使用更复杂的代码,可以在<mx:Script>标签中编写ActionScript代码,定义一个处理方法,然后注册给该属性。
    下面的例子展示了一个包含Button控件和TextArea控件的应用程序。Button的click属性包含了一个简单的事件监听器,他将设置TextArea控件的text属性值为Hello World。
<?xml version="1.0"?>
<!-- mxml/TriggerCodeExample.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Panel title="My Application" 
        paddingTop="10" 
        paddingBottom="10"
        paddingLeft="10" 
        paddingRight="10"
    >
        <mx:TextArea id="textarea1"/>
        <mx:Button label="Submit" click="textarea1.text='Hello World';"/>
    </mx:Panel>
</mx:Application>


The following example shows the code for a version of the application in which the event listener is contained in an ActionScript function in an <mx:Script> tag:

下面的例子展示了在<mx:Script>标签中,使用ActionScript代码定义了一个事件监听器。

<?xml version="1.0"?>
<!-- mxml/TriggerCodeExample2.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            private function hello():void {
                textarea1.text="Hello World";
            }
        ]]>
    </mx:Script>
    <mx:Panel title="My Application" 
        paddingTop="10" 
        paddingBottom="10"
        paddingLeft="10" 
        paddingRight="10"
    >
        <mx:TextArea id="textarea1"/>
        <mx:Button label="Submit" click="hello();"/>
    </mx:Panel>
</mx:Application>

For more information about using ActionScript with MXML, see “Using ActionScript” on page 37.

你可能感兴趣的:(xml,Flex,actionscript)