Flex自定义事件

<?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>



3采用as自定义事件

新建actionScript类

package event
{
import flash.events.Event;
public class MyEvent extends Event
{
//定义常量
public static const MYEVENT:String = "myevent";
//定义成员变量
private var eventObject:int ;
public function MyEvent(type:String,i:int, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.eventObject =i;
}
//返回该成员变量
public  function getEventObject():int
{
  return eventObject;
}
}
}

.......页面中调用

<?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[
import event.MyEvent;
protected function button1_clickHandler():void
{
var evt:MyEvent = new MyEvent(MyEvent.MYEVENT,2);
dispatchEvent(evt);
}
public function init():void
{
 this.addEventListener(MyEvent.MYEVENT,eventHandle);
 
}
public function eventHandle(e:MyEvent):void
{
  trace(e.getEventObject());
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button x="405" y="209" label="点击我" click="button1_clickHandler()">
</s:Button>
</s:Application>




猜你喜欢

转载自435870628.iteye.com/blog/1676545