<?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">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
//侦听鼠标移动到按钮上的事件
protected function bt_mouseOverHandler():void
{
//当鼠标移动上去的时候
bt.label = "哈哈";
}
//侦听鼠标移出按钮上的事件
protected function bt_mouseOutHandler():void
{
// 当鼠标移出的时候
bt.label="Click me";
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button id="bt" x="130" y="187" width="173" height="35" label="Click me" mouseOver="bt_mouseOverHandler()" mouseOut="bt_mouseOutHandler()"/>
</s:Application>
2 使用mxml自定义事件
<?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" creationComplete="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
protected function button1_clickHandler():void
{
//当点击按钮的时候派生另一个事件去处理
dispatchEvent(new Event("Clicked"));
}
//当应用程序完成的时候监听一个事件
private function init():void
{
this.addEventListener("Clicked",clickedHandle);
}
protected function clickedHandle(event:Event):void
{
// TODO Auto-generated method stub
trace("事件被监听到了");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Metadata>
//先在该标签中自定义一个事件
[Event(name="Clicked", type="flash.events.Event")]
</fx:Metadata>
<s:Button x="408" y="207" label="按钮" click="button1_clickHandler()">
</s:Button>
</s:Application>