有機肥

          綠色

          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 有機肥 閱讀(251) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年3月>
          26272829123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 贵南县| 武隆县| 玉门市| 武宁县| 修文县| 德昌县| 延吉市| 崇州市| 彰化县| 土默特右旗| 石嘴山市| 仙居县| 武夷山市| 上蔡县| 都江堰市| 夏河县| 仁寿县| 龙川县| 衡东县| 连云港市| 东台市| 榆林市| 桂平市| 宜昌市| 红桥区| 额敏县| 漳平市| 长顺县| 长岭县| 仁化县| 太保市| 富裕县| 泸溪县| 湘西| 齐齐哈尔市| 鲁甸县| 泾阳县| 金塔县| 佛教| 徐水县| 林口县|