Flex4之四种事件处理方式
首先我这个示例是针对按钮来说的,其实其他的组件大致也都一样
第一种:直接写在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>
最后把整个代码贴上
- <SPAN style="FONT-SIZE: medium"><?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/mx" minWidth="955" minHeight="600">
- <fx:Script>
- <![CDATA[
- import mx.controls.Alert;
- private function handleClick(event:MouseEvent):void
- {
- Alert.show('HelloWorld','提示');
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <s:Button x="69" y="82" label="按钮" click="Alert.show('HelloWorld','提示');"/>
- <s:Button x="170" y="82" label="按钮">
- <s:click>
- <![CDATA[
- Alert.show('HelloWorld','提示');
- ]]>
- </s:click>
- </s:Button>
- <s:Button x="287" y="84" label="按钮" click="handleClick(event)"/>
- <s:Button x="403" y="83" label="按钮" id="c" >
- <s:creationComplete>
- c.addEventListener(MouseEvent.CLICK, handleClick);
- </s:creationComplete>
- </s:Button>
- </s:Application>
- </SPAN>