flex4.6事件指派+組件+參數傳遞

來源:互聯網
上載者:User

<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    xmlns:s="library://ns.adobe.com/flex/spark"    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"   xmlns:hz="com.hz.*"   creationComplete="initHandler(event)"><fx:Script><![CDATA[import Event.AddStuEvent;import mx.collections.ArrayCollection;import mx.controls.Alert;import mx.events.FlexEvent;protected function initHandler(event:FlexEvent):void{stuInfo.stuColl=new ArrayCollection();addEventListener(AddStuEvent.ADD_USER,addBtn);}protected function addBtn(event:AddStuEvent):void{if(stuMana.stuName.text!=""&&stuMana.stuAge.text!=""){stuInfo.stuColl.addItem(event.object);}else{Alert.show("兩項都要填三!","溫馨提示");}}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:layout><s:HorizontalLayout gap="30" paddingBottom="30" paddingLeft="30" paddingRight="30" paddingTop="30"/></s:layout><hz:StuInfo id="stuInfo" width="50%" height="100%"/><hz:StuManager id="stuMana" width="50%" height="100%"/></s:Application>
上面是主application檔案:<?xml version="1.0" encoding="utf-8"?><s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"  xmlns:s="library://ns.adobe.com/flex/spark"  xmlns:mx="library://ns.adobe.com/flex/mx" title="新增學生" creationComplete="initHandler(event)"><fx:Script><![CDATA[import Event.AddStuEvent;import mx.events.FlexEvent;public function initHandler(event:FlexEvent):void{addBtn.addEventListener(MouseEvent.CLICK,addBtnHandler);}protected function addBtnHandler(event:MouseEvent):void{var object:Object=new Object();object={sName:stuName.text,sAge:stuAge.text};dispatchEvent(new AddStuEvent(AddStuEvent.ADD_USER,object))}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:Form width="100%" height="100%"><s:FormItem label="學生姓名:" required="true" width="100%"><s:TextInput id="stuName" width="100%"/></s:FormItem><s:FormItem label="學生年齡" required="true" width="100%"><s:TextInput id="stuAge" width="100%"/></s:FormItem></s:Form><s:controlBarContent><s:Button label="新增學生:" id="addBtn"/><s:Button label="修改資訊" id="updateBtn"/></s:controlBarContent><s:controlBarLayout><s:HorizontalLayout horizontalAlign="center" gap="50" paddingTop="9" paddingBottom="9"/></s:controlBarLayout></s:Panel>
   下面是自訂事件:定義了一個常量和要傳遞的參數,event屬性只設定了string,bubble預設為false。
package Event{import flash.events.Event;public class AddStuEvent extends Event{public static const ADD_USER:String="addUser"; private var _object:Object=null;public function AddStuEvent(type:String, object:Object){super(type,true);_object=object;}public function get object():Object{return _object;}public function set object(value:Object):void{_object = value;}override public function clone():Event{return new AddStuEvent(type,object);}}}


<?xml version="1.0" encoding="utf-8"?><s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"  xmlns:s="library://ns.adobe.com/flex/spark"  xmlns:mx="library://ns.adobe.com/flex/mx" title="學生列表" creationComplete="initHandler(event)"><fx:Script><![CDATA[import mx.collections.ArrayCollection;import mx.events.FlexEvent;[Bindable]public var stuColl:ArrayCollection=null;protected function initHandler(event:FlexEvent):void{// TODO Auto-generated method stub}private function stuArr(item:Object):String{var str:String=item.sName+"哈哈"+item.sAge;return str;}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:List id="stuList" width="100%" height="100%" dataProvider="{stuColl}" labelFunction="stuArr"></s:List><s:controlBarContent><s:Button label="刪除學生" id="deleteBtn"/></s:controlBarContent></s:Panel>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.