Flex事件機制的工作流程
事件機制的工作流程
關于事件流
當事件發(fā)生后生成一個攜帶數(shù)據(jù)的對象,然后檢查目標對象是否存在顯示層中,并遍歷從根容器一直到目標對象所在位置的所有對象,以樹形勢表示。自動檢測所經過的節(jié)點是否注冊了監(jiān)聽器。
事件流暗運行流程分為3步:
- 捕獲階段:捕獲事件 capturing,從根節(jié)點開始順序而下,檢測每個節(jié)點是否注冊了監(jiān)聽器。同時,Flex 將事件對象的currentTarget 值改為當前正在檢測的對象。如果注冊了監(jiān)聽器,則調用監(jiān)聽函數(shù)。
- 目標階段:檢測目標的監(jiān)聽器 targeting:觸發(fā)在目標對象本身注冊的監(jiān)聽程序
- 冒泡階段:事件冒泡 bubbling:從目標節(jié)點到根節(jié)點,檢測每個節(jié)點是否注冊了監(jiān)聽器,如果有,則調用監(jiān)聽函數(shù)。
每個事件對象都有以下屬性:
target:事件的派發(fā)者
currentTarget:當前正在檢測的的對象,幫助跟蹤事件傳播的過程。
默認情況下,捕獲功能處于關閉狀態(tài),一般沒有必要進行捕獲跟蹤。
事件只在bubbles 屬性為true 時才進行冒泡,可以冒泡的事件包括:change、click、doubleClick、keyDown、keyUp、mouseDown、 mouseUp。并且不能在一個監(jiān)聽器中同時打開捕獲和冒泡功能,要做到這一點,只能注冊兩個監(jiān)聽器,分別實現(xiàn)。
現(xiàn)在來看一個例子:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
- <mx:Style source="style.css" />
- <mx:Script>
- <![CDATA[
- import flash.events.MouseEvent;
- internal function initApp():void{
- canvas_1.addEventListener(MouseEvent.CLICK,pressBtn,true);
- canvas_2.addEventListener(MouseEvent.CLICK,pressBtn);
- btn_1.addEventListener(MouseEvent.CLICK,pressBtn);
- btn_2.addEventListener(MouseEvent.CLICK,pressBtn);
- }
- internal function output(msg:String):void{
- debug_txt.text += msg+"\n";
- }
- internal function pressBtn(evt:MouseEvent):void{
- output("是否冒泡--"+evt.bubbles);
- output("目標對象-- "+evt.target+" -- "+evt.eventPhase);
- output("遍歷對象-- "+evt.currentTarget);
- output("------------");
- }
- ]]>
- </mx:Script>
- <mx:Canvas id = "canvas_1" styleName="box" x="37" y="63" width="445" height="216">
- <mx:Text x="13" y="10" text="Canvas_1"/>
- <mx:Canvas id="canvas_2" styleName="box" x="10" y="102" width="173" height="90">
- <mx:Text x="10" y="10" text="Canvas_2"/>
- <mx:Button id = "btn_2" x="10" y="38" label="Button_2"/>
- </mx:Canvas>
- <mx:Button id="btn_1" x="16" y="38" label="Button_1"/>
- </mx:Canvas>
- <mx:TextArea id="debug_txt" styleName="textBox" x="37" y="304" height="198" width="445"/>
- </mx:Application>
- target:派發(fā)事件的目標對象
- currentTarget:事件流當前正經過的目標對象
- bubbles:是否打開了冒泡功能
- eventPhase:事件流當前的階段,1:捕獲,2:目標,3:冒泡
addEventListener(
type:String, 事件的類型
listener:Function, 監(jiān)聽函數(shù)
useCapture:Boolean = false, 是否打開捕獲功能
priority:int = 0, 監(jiān)聽器優(yōu)先級別
useWeakReference:Boolean = false 是否使用弱引用
)
如果useCapture 為true,打開了捕獲功能,則該組件的冒泡階段被取消。
只有可視化的對象有3個階段,而像XML等非可視化對象只有目標階段。
8.2.2 事件對象
Event對象中包含目標對象存放的數(shù)據(jù),這些數(shù)據(jù)都成為Event的屬性,以供偵聽器使用:
Event的屬性:
- bubbles:只讀,布爾,事件是否開啟冒泡功能
- cancelable:只讀,布爾,處理事件的默認行為是否可以停止。主要針對一些系統(tǒng)事件,如果值為true,則Event的preventDefault方法可以使用,否則不可用。
- currentTarget:只讀,對象,當前正在調用監(jiān)聽器的對象
- eventPhase:只讀,整數(shù),返回事件流正經歷的階段。1:捕獲,2:目標,3:冒泡
- target:只讀,派發(fā)事件的目標對象
- type:只讀,字符,事件類型。比如鼠標點擊事件的類型:click,并被定義為常量:MouseEvent.CLICK
構造函數(shù):
Event(
type:String, 事件類型
bubbles:Boolean = false, 是否冒泡
cancelable:Boolean = false 是否可以停止
)
Event 的方法:
- isDefaultPrevented:判斷preventDefault 是否已經被調用
- preventDefault:停止事件的默認行為。針對一些系統(tǒng)事件,cancelable為true時才可用。
- stopImmediatePropagation:停止當前的事件流傳播,包括當前正在處理的對象
- stopPropagation:停止當前的事件流傳播,但不會停止當前正在處理的對象
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
- <mx:Style source="style.css" />
- <mx:Script>
- <![CDATA[
- import flash.events.MouseEvent;
- internal function initApp():void{
- canvas_1.addEventListener(MouseEvent.CLICK,CanvasHandler);
- canvas_2.addEventListener(MouseEvent.CLICK,CanvasHandler);
- canvas_2.addEventListener(MouseEvent.CLICK,pressBtn);
- btn_1.addEventListener(MouseEvent.CLICK,pressBtn);
- }
- internal function output(msg:String):void{
- debug_txt.text += msg+"\n";
- }
- internal function pressBtn(evt:MouseEvent):void{
- output("是否冒泡--"+evt.bubbles);
- output("目標對象-- "+evt.target+" -- "+evt.eventPhase);
- output("遍歷對象-- "+evt.currentTarget);
- output("------------");
- }
- internal function CanvasHandler(evt:MouseEvent):void{
- output("目標對象-- "+evt.currentTarget+" -- "+evt.eventPhase);
- //停止事件流的傳播
- evt.stopImmediatePropagation();
- //evt.stopPropagation();
- }
- ]]>
- </mx:Script>
- <mx:Canvas id = "canvas_1" styleName="box" x="37" y="63" width="425" height="160">
- <mx:Text x="13" y="10" text="Canvas_1"/>
- <mx:Canvas id="canvas_2" styleName="box" x="10" y="52" width="173" height="90">
- <mx:Text x="10" y="10" text="Canvas_2"/>
- <mx:Button id = "btn_1" x="10" y="38" label="Button_1"/>
- </mx:Canvas>
- </mx:Canvas>
- <mx:TextArea id="debug_txt" styleName="textBox" x="37" y="245" height="198" width="425"/>
- </mx:Application>
8.2.3 偵聽和響應事件--一個偵聽鍵盤事件的例子
this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
注冊鍵盤按下事件,交給keyHandler處理,也可以在Application標簽添加事件:
keyDown="keyHandler(event)" 這種情況無法移除事件。
注冊了事件監(jiān)聽器,使用完畢后,必須使用removeEcentListener 方法刪除監(jiān)聽函數(shù):
removeEcentListener(
type:String, 事件類型
listener:Function, 監(jiān)聽函數(shù)
useCapture:Boolean = false 是否開啟捕獲功能,如果注冊時打開,移除也要打開。
)
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
- creationComplete="initApp()">
- <mx:Style source="style.css" />
- <mx:Script>
- <![CDATA[
- import flash.events.KeyboardEvent;
- internal function initApp():void{
- this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
- }
- private function keyHandler(e:KeyboardEvent):void{
- var str:String = "你按下的是: "+e.keyCode;
- debug_txt.text += str +"\n";
- }
- ]]>
- </mx:Script>
- <mx:TextArea id="debug_txt" styleName="textBox" x="25" y="78" height="198" width="212" editable="false"/>
- <mx:Text x="25" y="50" text="按鍵盤上的任意鍵"/>
- </mx:Application>
posted on 2008-08-22 10:17 gembin 閱讀(660) 評論(0) 編輯 收藏 所屬分類: Flex