有机肥

绿色

flex event(zhuan)

  1. package com.montage.vo  

  2.     public class User  
  3.     {  
  4.         public function User() {}  
  5.         public var name:String;  
  6.         public var country:String; 
  7.     }  
  8. }  
package com.montage.events  
  1. {  
  2.     import com.montage.vo.User;       
  3.     import flash.events.Event;  
  4.     public class UserEvent extends Event  
  5.     {  
  6.         public static var SELECTED_USER:String = "selectedUser";           
  7.         public var user:User;           
  8.         public function UserEvent(type:String,   bubbles:Boolean=false, cancelable:Boolean=false)  
  9.         {  
  10.             super(type, bubbles, cancelable);  
  11.         }  
  12.     }  
  13. }  

  1. package com.montage.model  
  2. {  
  3.     import flash.events.Event;  
  4.     import flash.events.EventDispatcher;
  5.  
  6.     [Event(name="selectedUser", type="com.montage.events.UserEvent")]
  7.     public class EventMessage extends EventDispatcher  
  8.     { 
  9.         private static var instance:EventMessage = null;  
  10.         public function EventMessage()  
  11.         {  
  12.             if( instance != null )  
  13.             {  
  14.                 throw new Error("EventMessage是一个单件类,只能被实例化一次!");  
  15.             }  
  16.         }  
  17.           
  18.         public static function getInstance():EventMessage  
  19.         {  
  20.             if( instance == null )  
  21.             {  
  22.                 instance = new EventMessage();  
  23.             }  
  24.             return instance;  
  25.         }      
  26.         public function send( event:Event ):void  
  27.         {  
  28.             dispatchEvent( event );  
  29.         }  
  30.     }  
  31. }  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300" title="选择用户">  
  3.     <mx:Script>  
  4.         <![CDATA[ 
  5.             import mx.controls.Alert; 
  6.             import com.montage.events.UserEvent; 
  7.             import com.montage.vo.User; 
  8.             import com.montage.model.EventMessage; 
  9.             import mx.managers.PopUpManager; 
  10.              
  11.             private var eMessage:EventMessage = EventMessage.getInstance(); 
  12.              
  13.             private function submitHandler():void 
  14.             { 
  15.                 if( grid.selectedIndex > -1 ) 
  16.                 { 
  17.                     var item:XML = XML( grid.selectedItem );
  18.                     var user:User = new User(); 
  19.                     user.name = item.@name; 
  20.                     user.country = item.@country;
  21.                     var event:UserEvent = new UserEvent( UserEvent.SELECTED_USER ); 
  22.                     event.user = user;
  23.                     eMessage.send( event ); 
  24.                     cancelHandler(); 
  25.                 } 
  26.                 else  { 
  27.                     Alert.show("请选择一个用户!"); 
  28.                 } 
  29.             } 
  30.              
  31.             private function cancelHandler():void 
  32.             { 
  33.                 PopUpManager.removePopUp( this ); 
  34.             } 
  35.         ]]>  
  36.     </mx:Script>  
  37.     <mx:XML id="users" source="user.xml"/>  
  38.     <mx:DataGrid id="grid" width="100%" height="100%" dataProvider="{users.User}">  
  39.         <mx:columns>  
  40.             <mx:DataGridColumn headerText="姓名" dataField="@name"/>  
  41.             <mx:DataGridColumn headerText="国家" dataField="@country"/>  
  42.         </mx:columns>  
  43.     </mx:DataGrid>  
  44.     <mx:ControlBar width="100%">  
  45.         <mx:Spacer width="100%"/>  
  46.         <mx:Button label="确定" click="submitHandler()"/>  
  47.         <mx:Button label="取消" click="cancelHandler()"/>  
  48.     </mx:ControlBar>  
  49. </mx:TitleWindow>  
           <root>  
  1.     <User name="David" country="America"/>  
  2.     <User name="Tome" country="Canada"/>  
  3.     <User name="Montage" country="China"/>  
  4. </root>
        <?xml version="1.0" encoding="utf-8"?>  
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" fontSize="12">  
  2.     <mx:Script>  
  3.         <![CDATA[ 
  4.             import com.montage.events.UserEvent; 
  5.             import com.montage.model.EventMessage; 
  6.             import mx.managers.PopUpManager; 
  7.             import com.montage.view.user.UserList; 
  8.              
  9.             private var eMessage:EventMessage = EventMessage.getInstance(); 
  10.              
  11.             private function init():void 
  12.             { 
  13.                 eMessage.addEventListener(UserEvent.SELECTED_USER, selectedUserHandler); 
  14.             } 
  15.              
  16.            
  17.             private function selectedUserHandler( event:UserEvent ):void 
  18.             { 
  19.                 textArea.htmlText = "你选择了:<b>" + event.user.name + "</b>("+ event.user.country +")"; 
  20.             } 
  21.            
  22.             private function clickHandler():void 
  23.             { 
  24.                 var userList:UserList = UserList( PopUpManager.createPopUp(this, UserList, true) ); 
  25.                 userList.x = ( width - userList.width ) / 2; 
  26.                 userList.y = ( height - userList.height ) / 2; 
  27.             } 
  28.              
  29.         ]]>  
  30.     </mx:Script>  
  31.     <mx:Panel width="400" height="300" layout="vertical" paddingLeft="5" paddingRight="5" paddingTop="5">  
  32.         <mx:TextArea id="textArea" width="100%"/>  
  33.         <mx:Button label="选择用户" click="clickHandler()"/>  
  34.     </mx:Panel>  
  35. </mx:Application>

posted on 2012-03-20 11:17 有机肥 阅读(249) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜