SandboxMouseEvent, FlexMouseEvent,Flex 多选 ComboBox

说多了墨迹,直接帖代码

 

MyClassFactory 类 如下:

[java]  view plain copy
  1. import flash.display.DisplayObject;  
  2.   
  3. import mx.core.ClassFactory;  
  4. import mx.states.OverrideBase;  
  5.   
  6. public class MyClassFactory extends ClassFactory  
  7. {  
  8.     public function MyClassFactory(generator:Class=null)  
  9.     {  
  10.         super(generator);  
  11.     }  
  12.     private var eventArray:Array=new Array();  
  13.     private var _properties:Array=new Array();  
  14.     public function addInitEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0,useWeakReference:Boolean=false):void{  
  15.         eventArray.push({type:type,listener:listener,useCapture:useCapture,priority:priority,useWeakReference:useWeakReference});  
  16.     }  
  17.     public function addInitProperties(key:String,value:*):void{  
  18.         _properties.push({key:key,value:value});  
  19.     }  
  20.       
  21.     override public function newInstance():*{  
  22.         trace("MyClassFactory.newInstance();");  
  23.         var obj:*=super.newInstance();  
  24.         var event:Object;  
  25.         if(eventArray!=null&&eventArray.length>0&&obj as DisplayObject){  
  26.             var i:int=0;  
  27.             for(i=0;i<_properties.length;i++){  
  28.                 event=_properties[i];  
  29.                 if(event.value is Function){  
  30.                     obj[event.key]=event.value();  
  31.                 }else{  
  32.                     obj[event.key]=event.value;  
  33.                 }  
  34.                   
  35.             }  
  36.               
  37.             for(i=0;i<eventArray.length;i++){  
  38.                 event=eventArray[i];  
  39.                 DisplayObject(obj).addEventListener(event.type,event.listener,event.useCapture,event.priority,event.useWeakReference);  
  40.             }  
  41.         }  
  42.         return obj  
  43.     }  
  44.       
  45. }  

 

 

MyComboBox 类 如下:

 

[java]  view plain copy
  1. import flash.events.Event;  
  2. import flash.events.MouseEvent;  
  3. import mx.collections.ArrayCollection;  
  4. import mx.controls.ComboBox;  
  5. import mx.controls.List;  
  6. import mx.controls.listClasses.ListBase;  
  7. import mx.core.ClassFactory;  
  8. import mx.events.FlexEvent;  
  9. import mx.events.FlexMouseEvent;  
  10. import mx.events.ListEvent;  
  11. import mx.events.SandboxMouseEvent;  
  12.   
  13. public class MyComboBox extends ComboBox  
  14. {  
  15.     public function MyComboBox()  
  16.     {  
  17.         super();  
  18.         this.dropdownFactory=new MyClassFactory(List);  
  19.         this.getDropdownFactory().addInitEventListener(ListEvent.CHANGE,dropdown_Change,false,1);  
  20.         this.getDropdownFactory().addInitEventListener(MouseEvent.MOUSE_DOWN, dropdown_mouseDownHandler);  
  21.         this.getDropdownFactory().addInitEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, dropdown_mouseOutsideHandler);  
  22.         this.getDropdownFactory().addInitEventListener(FlexMouseEvent.MOUSE_WHEEL_OUTSIDE, dropdown_mouseOutsideHandler);  
  23.         this.getDropdownFactory().addInitEventListener(SandboxMouseEvent.MOUSE_DOWN_SOMEWHERE, dropdown_mouseOutsideHandler);  
  24.         this.getDropdownFactory().addInitEventListener(SandboxMouseEvent.MOUSE_WHEEL_SOMEWHERE, dropdown_mouseOutsideHandler);  
  25.         this.getDropdownFactory().addInitProperties("allowMultipleSelection",true);  
  26.         this.getDropdownFactory().addInitProperties("selectedItems",getSelectedItems);  
  27.     }  
  28.   
  29.     private var _selectedItems:Array;  
  30.     private var _selectedIndices:Array;  
  31.     private var _separation:String=",";  
  32.     [Bindable("change")]  
  33.     [Bindable("valueCommit")]  
  34.     public function get selectedItems():Array{  
  35.         return  _selectedItems==null?[]:_selectedItems;  
  36.     }  
  37.     public function set selectedItems(array:Array):void{  
  38.         var t:String;  
  39.         this._selectedItems=array;  
  40.         this.text=selectedLabel;  
  41.     }  
  42.     [Bindable("change")]  
  43.     [Bindable("valueCommit")]  
  44.     public function get selectedIndices():Array{  
  45.         return _selectedIndices!=null?_selectedIndices:[];  
  46.     }  
  47.     public function set selectedIndices(array:Array):void{  
  48.         this._selectedIndices=array;  
  49.     }  
  50.     /** 
  51.      * Label 分隔符 
  52.      * */  
  53.     public function get separation():String{  
  54.         return this._separation;  
  55.     }  
  56.     public function set separation(value:String):void{  
  57.         this._separation=value;  
  58.     }  
  59.     public function getSelectedItems():Array{  
  60.         return this.selectedItems;  
  61.     }  
  62.     /** 
  63.      * 选中项时,Ctrl 是否按下 
  64.      * */  
  65.     private var ctrlKey:Boolean=false;  
  66.   
  67.     private function dropdown_Change(event:ListEvent):void{  
  68.         var listBase:ListBase=event.currentTarget as ListBase;  
  69.         this.selectedItems=listBase.selectedItems;  
  70.         this.selectedIndices=listBase.selectedIndices;  
  71.           
  72.         trace("dropdown_Change-----"+"selectedItems:"+selectedItems.length+"\t"+"selectedIndices:"+selectedIndices.length);  
  73.           
  74.     }  
  75.       
  76.     private function dropdown_mouseDownHandler(event:MouseEvent):void{  
  77.         ctrlKey=event.ctrlKey;  
  78.     }  
  79.     private function dropdown_mouseDownOutSide(event:FlexMouseEvent):void{  
  80.         close();  
  81.     }  
  82.       
  83.     override public function close(trigger:Event=null):void{  
  84.         if(!ctrlKey){  
  85.             super.close(trigger);//未按下 Ctrl 时 关闭  
  86.         }  
  87.     }  
  88.     override public function get selectedLabel():String  
  89.     {  
  90.         var array:Array=this.selectedItems;  
  91.         var result:String="";  
  92.         for(var i:int=0;i<array.length;i++){  
  93.             result+=itemToLabel(array[i]);  
  94.             if(i!=(array.length-1)){  
  95.                 result+=separation;  
  96.             }  
  97.         }  
  98.           
  99.         return result;  
  100.     }  
  101.     public function  getDropdownFactory():MyClassFactory{  
  102.         return super.dropdownFactory as MyClassFactory;  
  103.     }  
  104.     /** 
  105.      *  @private 
  106.      */  
  107.     private function dropdown_mouseOutsideHandler(event:Event):void  
  108.     {  
  109.         trace("selectedItems:"+selectedItems.length+"\t"+"selectedIndices:"+selectedIndices.length);  
  110.         if (event is MouseEvent)  
  111.         {  
  112.             var mouseEvent:MouseEvent = MouseEvent(event);  
  113.             if (!hitTestPoint(mouseEvent.stageX, mouseEvent.stageY, true))  
  114.             {  
  115.                 super.close(event);  
  116.             }  
  117.         }  
  118.         else if (event is SandboxMouseEvent)   
  119.         {  
  120.             super.close(event);  
  121.         }  
  122.     }  
  123. }  


调用代码 如下:

[html]  view plain copy
  1. <my:MyComboBox width="180">  
  2.     <my:dataProvider>  
  3.         <s:ArrayCollection>  
  4.             <fx:String>选项一</fx:String>  
  5.             <fx:String>选项二</fx:String>  
  6.             <fx:String>选项三</fx:String>  
  7.             <fx:String>选项四</fx:String>  
  8.             <fx:String>选项五</fx:String>  
  9.         </s:ArrayCollection>  
  10.     </my:dataProvider>  
  11. </my:MyComboBox>  


(按Ctrl 点击选择项 为多选)


转载:http://blog.csdn.net/hellofr/article/details/7026841




你可能感兴趣的:(SandboxMouseEvent, FlexMouseEvent,Flex 多选 ComboBox)