Flex4之四种事件处理方式

时间:2021-03-24 22:41:23

首先我这个示例是针对按钮来说的,其实其他的组件大致也都一样


第一种:直接写在click属性中
<s:Button label="click me" click="Alert.show('clicked!')"/> 

第二种:嵌入<s:click>标签
  <s:Button id="b" label="click me once">
            <s:click>
                b.enabled = false;
                mx.controls.Alert.show('clicked!');
            </s:click>
        </s:Button>
第三种:普通方法处理

  <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
           
            private function handleClick(event:MouseEvent):void
            {
                b.enabled = false;
                mx.controls.Alert.show('clicked!');
            }
        ]]>
    </fx:Script>


第四种:添加事件监听器
 <s:Button id="b" label="click me once">
            <s:creationComplete>
                b.addEventListener(MouseEvent.CLICK, handleClick);
            </s:creationComplete>
        </s:Button>
  <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
           
            private function handleClick(event:MouseEvent):void
            {
                b.enabled = false;
                mx.controls.Alert.show('clicked!');
            }
        ]]>
    </fx:Script>

 

最后把整个代码贴上

 

Xml代码 Flex4之四种事件处理方式  Flex4之四种事件处理方式Flex4之四种事件处理方式
  1. <SPAN style="FONT-SIZE: medium"><?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    
  3.                xmlns:s="library://ns.adobe.com/flex/spark"    
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
  5.     <fx:Script>  
  6.         <![CDATA[  
  7.             import mx.controls.Alert;  
  8.               
  9.             private function handleClick(event:MouseEvent):void  
  10.             {  
  11.                 Alert.show('HelloWorld','提示');   
  12.             }  
  13.         ]]>  
  14.     </fx:Script>  
  15.     <fx:Declarations>  
  16.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  17.        
  18.     </fx:Declarations>  
  19.     <s:Button x="69" y="82" label="按钮" click="Alert.show('HelloWorld','提示');"/>  
  20.     <s:Button  x="170" y="82" label="按钮">  
  21.      <s:click>  
  22.          <![CDATA[  
  23.          Alert.show('HelloWorld','提示');   
  24.          ]]>  
  25.      </s:click>  
  26.     </s:Button>  
  27.     <s:Button  x="287" y="84" label="按钮"  click="handleClick(event)"/>  
  28.     <s:Button x="403" y="83" label="按钮" id="c" >  
  29.         <s:creationComplete>  
  30.             c.addEventListener(MouseEvent.CLICK, handleClick);   
  31.         </s:creationComplete>  
  32.     </s:Button>  
  33.        
  34.        
  35. </s:Application>  
  36. </SPAN>