package event
{
import flash.events.Event;
import spark.components.Button;
[Event(name="test", type="flash.events.Event")]
[Event(name="demo", type="flash.events.Event")]
public class EventPriorityButton extends Button
{
public function EventPriorityButton()
{
super();
this.addEventListener("test", test1);
this.addEventListener("test", test2);
this.addEventListener("demo", demo2);
this.addEventListener("demo", demo1);
}
private function test1(event:Event):void
{
trace("EventPriorityButton.test1");
}
private function test2(event:Event):void
{
trace("EventPriorityButton.test2");
}
private function demo1(event:Event):void
{
trace("EventPriorityButton.demo1");
}
private function demo2(event:Event):void
{
trace("EventPriorityButton.demo2");
}
}
}
<?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"
xmlns:event="event.*"
creationComplete="eventPriorityTestApp_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function eventPriorityTestApp_creationCompleteHandler(event:FlexEvent):void
{
eventPriorityTestButton.addEventListener("test", test3);
eventPriorityTestButton.addEventListener("test", test4, false, 20000);
eventPriorityTestButton.addEventListener("demo", demo3);
eventPriorityTestButton.addEventListener("demo", demo4, false, 10000);
}
protected function eventPriorityTestButton_clickHandler(event:MouseEvent):void
{
eventPriorityTestButton.dispatchEvent(new Event("demo"));
eventPriorityTestButton.dispatchEvent(new Event("test"));
}
protected function test3(event:Event):void
{
trace("EventPriorityApp.test3");
}
protected function test4(event:Event):void
{
trace("EventPriorityApp.test4");
}
protected function demo3(event:Event):void
{
trace("EventPriorityApp.demo3");
}
protected function demo4(event:Event):void
{
trace("EventPriorityApp.demo4");
}
]]>
</fx:Script>
<event:EventPriorityButton id="eventPriorityTestButton"
label="事件优先级测试"
click="eventPriorityTestButton_clickHandler(event)"/>
</s:Application>
◇[b]测试输出结果:[/b]
[list]
[*]EventPriorityApp.demo4
[*]EventPriorityButton.demo2
[*]EventPriorityButton.demo1
[*]EventPriorityApp.demo3
[*]EventPriorityApp.test4
[*]EventPriorityButton.test1
[*]EventPriorityButton.test2
[*]EventPriorityApp.test3
[/list]
◇[b]测试总结:[/b]
[list]
[*]优先级对同一类型事件有效,对不同类型事件无效,虽然test4优先级20000高于demo4优先级10000,但是先派发demo事件,所有demo4还是先执行了
[*]事件类型相同优先级相同的话谁先注册就先执行谁
[/list]