Flex builder学习笔记
第五章 Actionscript3.0基础 5.4.4确定循环 2,for...in语句 ? for...in语句用来在对象属性或数组元素中做迭代循环 var arrName:array = ["Mark","Dasiy","Doug"]; for(var i:string in arrName) { trace(arrName[i]); }输出结果: Mark Dasiy Doug 3.for each...in 语句 5.5.3 set和get函数 第六章 事件驱动编程 6.4 EventDispatcher类 EventDispatcher类通常用到的方法 ? addEventListener方法: 为EventDispatcher对象注册事件侦听器; ? dispatchEvent方法:触发事件,也可称为调度事件; ? hasEventListener方法:检查对象是否为特定的事件注册了事件监听器; ? removeEventListener方法:从EventDispatcher对象中删除事件侦听器。 6.4.1 注册事件侦听器 1)使用addEventListener方法 public function addEventListener (type:String,listener:Function,useCapture:Boolean = false,priority:int = 0,useWeakReference:Boolean = false) : void type:String:必需的参数,说明了侦听器所侦听的事件类型; listener:Function:必需的参数,负责在事件发生后响应事件。flex中侦听器只接受Event对象作为唯一参数,且无返回值,即 function(evt:Event):voiduseCapture:Boolean:可选参数,true为在捕获阶段处理事件,若要捕获、目标、冒泡三个阶段都处理事件,需调用两次addEventListener,一次为true一次为false; priority:int :可选参数,设置监听优先级; 2)通过MXML标签设置事件侦听器 <mx:TagName eventName="ActionScript代码段或事件侦听器函数"/> 6.4.2 触发事件:dispatchEvent ? 所谓触发事件,就是通知Flash Player把事件对象调度到从显示列表根节点开始的事件流中。 ? public function dispatchEvent (event:Event) : Boolean 6.6 自定义事件代码样例 6.6.2 步骤一:创建自定义事件类 1,新建 ActionScript类SwitchLightEvent,以Event为基类; 2,定义私有属性; 3,编写构造函数:super(varType) ;为私有属性赋值; 4,重载clone()函数,返回一个新的SwitchLightEvent对象; 5,编写getSelectedLight()函数,返回私有属性的值; 代码为: package events { import flash.events.Event; public class SwitchLightEvent extends Event { private var selectedLight:String; public function SwitchLightEvent(varType:String,varLight:String){ super(varType); this.selectedLight = varLight; } override public function clone():Event{ return new SwitchLightEvent(type,selectedLight); } public function getSelectedLight():String{ return selectedLight; } } } 6.6.3 步骤二:利用[Event]定义事件 6.6.4 步骤三:触发事件 <?xml version="1.0" encoding="utf-8"?> <!--LightConsole.mxml--> <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="30" horizontalAlign="center" fontSize="16" > <mx:Metadata> [Event(name="switchLightEvent",type="events.SwitchLightEvent")] <!--利用[Event]定义事件--> </mx:Metadata> <mx:Script> <![CDATA[ import events.SwitchLightEvent; private function clickEventHandler(varSelectedLight:String):void{ var evtObj:SwitchLightEvent = new SwitchLightEvent("switchLightEvent",varSelectedLight); dispatchEvent(evtObj); <!--创建事件对象,并触发该事件--> } ]]> </mx:Script> <mx:Button label="绿色" id="btnGreen" click="clickEventHandler('交通信号灯:绿色')"/> <mx:Button label="红色" id="btnRed" click="clickEventHandler('交通信号灯:红色')"/> <mx:Button label="蓝色" id="btnBlue" click="clickEventHandler('交通信号灯:蓝色')"/> </mx:HBox> 6.6.5 步骤四:注册SwitchLightEvent的事件侦听器 1,在主程序中设置LightConsole实例的事件属性switchLightEvent值为函数switchEventHandler(event); 2,定义switchEventHandler(event)方法,获取使用event.getSelectedLight()获取事件属性,并赋值给selectedLight,而该值绑定在Light的currentLight中,而在Light中currentLight绑定Label的text,实现更新字符显示。 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" xmlns:comp="view.*"> <mx:Script> <![CDATA[ import events.SwitchLightEvent; [Bindable] private var selectedLight:String; private function switchEventHandler(event:SwitchLightEvent):void{ selectedLight = String(event.getSelectedLight()); } ]]> </mx:Script> <comp:Lights currentLight="{selectedLight}"/> <comp:LightConsole switchLightEvent = "switchEventHandler(event)"/> </mx:Application> 第七章 基于组件的开发
7.2 MXML设置组件属性
1,设置属性
内置标签属性
<mx:Button label="按键样例" fontSize="9"/>
子标签属性
<mx:Button> <mx:fontSize>9</mx:fontSize> <mx:label>按键样例<、mx:label> </mx:Button>
2.属性值可设置为ActionScript代码
<mx:Button label="按键样例" click="mx.controls.Alert.show('按键被点击')"/>
7.3 简述数据绑定
数据绑定
数据源->绑定目标
方法:
1),使用{}
2),使用<mx:Binding source="txtSourece.text" destination="txtTarget.text"/>
7.4 组件概览
Flex 的组件:容器、导航和工具控件
7.5 自定义MXML组件
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml " layout="vertical " horizontalAlign=" center " xmlns:comp="view.* " > ...... <comp:Lights currentLight="{ selectedLight }" /> <comp:LightConsole switchLightEvent = "switchEventHandler(event)"/> </mx:Application>
xmlns:comp="view.*?"为所有view目录下的组件定义了命名空间comp
7.5.2定义MXML的属性:public和Set/Get存取器
1,public属性使用
[Bindable]绑定主应用的变量到子程序的public属性上,实现对子程序属性的控制
2,Set和Get存取器
<span style="font-size:12px;">... <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; private var _productModelData:ArrayCollection; //productModelData的Set方法 public function set productModelDate(val:ArrayCollection): void{ __productModelData = val; dispatchEvent( new Event("productModelDataChanged" )); } //使用[Bindable]元数据定义Get方法事件绑定 [ Bindable (event= "productModelDataChanged")] //productModelData的Get方法 public function get productModelData():ArrayCollection{ return _productModelData; } ]]> </mx:Script> ...</span>
[Bindable (event="productModelDataChanged")]保证了Set的同时会调用Get以返回_productModelData
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |