flex 事件 一處分發,任意監聽

代碼:數組

  
  
  
  
  1. package com.evrnt  
  2. {  
  3.     import flash.events.Event;  
  4.     import flash.events.EventDispatcher;  
  5.     import flash.events.IEventDispatcher;  
  6.  
  7.     public class InputEventDispatcher   {  
  8.           
  9.         private static var _inst:InputEventDispatcher;  
  10.         private var eventDispatcher:IEventDispatcher;  
  11.           
  12.         public function InputEventDispatcher(target:IEventDispatcher = null){  
  13.             eventDispatcher = new EventDispatcher(target);  
  14.         }  
  15.           
  16.         /** 獲取FastReportEventDispatcher類的惟一實例 */  
  17.         public static function getInstance():InputEventDispatcher{  
  18.             if(!_inst){  
  19.                 _inst = new InputEventDispatcher();  
  20.             }  
  21.             return _inst;  
  22.         }  
  23.           
  24.         /** 添加事件偵聽器函數 */  
  25.         public function addEventListener(type:String,listener:Function,useCapture:Boolean=false,pririty:int=0,useWeakReference:Boolean=true):void{  
  26.             eventDispatcher.addEventListener(type,listener,useCapture,pririty,useWeakReference);  
  27.         }  
  28.           
  29.         /** 刪除事件監聽器函數 */  
  30.         public function removeEventListener(type:String,listener:Function,useCapture:Boolean=false,priority:int=0,useWeakReference:Boolean=true):void{  
  31.             eventDispatcher.removeEventListener(type,listener,useCapture);  
  32.         }  
  33.           
  34.         /** 事件派發函數 */  
  35.         public function dispatchEvent(event:InputEvent):Boolean{  
  36.             return eventDispatcher.dispatchEvent(event);  
  37.         }  
  38.           
  39.         /** 檢查是否已經存在事件偵聽器 */  
  40.         public function hasEventListener(type:String):Boolean{  
  41.             return eventDispatcher.hasEventListener(type);  
  42.         }  
  43.           
  44.         /** 檢查eventDispatcher 對象或其餘任何數組是否註冊了指定類型的事件偵聽器 */  
  45.         public function willTrigger(type:String):Boolean{  
  46.             return eventDispatcher.willTrigger(type);  
  47.         }  
  48.     }  

自定義一個類用來發送和監聽事件,就能夠實如今任意地方監聽。ide

相關文章
相關標籤/搜索