Flex4教程:添加事件的3种方法

http://www.webjx.com/  2010-03-13 06:54:22  来源:闪吧个人空间 作者:sliz

Webjx核心提示:Flex4教程:添加事件的3种方法.

1,直接写在click属性里
2,在click属性里写事件的处理函数
3,用addEventListener添加事件
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"
               initialize="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function init():void{
                btn3.addEventListener(MouseEvent.CLICK,onClick);
            }
            private function onClick(e:MouseEvent):void{
                if(e.target==btn3)
                    label.text+="按钮3点击\n";
                else
                    label.text+="按钮2点击\n";
            }
        ]]>
    </fx:Script>
    <s:VGroup>
        <s:Button label="按钮" click="label.text+=’按钮1点击\n’" />
        <s:Button label="按钮2" click="onClick(event)"></s:Button>
        <s:Button label="按钮3" id="btn3"></s:Button>
        <s:Label id="label"></s:Label>
    </s:VGroup>
</s:Application>
更多