<!-- Demo_事件绑定(MXML中的绑定) -->
<?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="1024" minHeight="768" pageTitle="TheStudioOfCenyebao">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable] //[Bindable]元素据的用意就是监视这个变量的任何变化。
protected var _labelText:String = "Label befor event!";
]]>
</fx:Script>
<!--view-->
<s:VGroup width="350" height="120" horizontalCenter="0" verticalCenter="0" paddingLeft="20" paddingTop="20">
<s:Label id="myLbl" text="{_labelText}"/>
<s:Button id="myBtn" label="ChangeLabel">
<s:click>
<![CDATA[
_labelText = "Label changed(" + Math.round(Math.random()*10) + ")!";
]]>
</s:click>
</s:Button>
</s:VGroup>
</s:Application>
<!-- Demo_事件绑定(使用ChangeWatcher的ActionScript绑定) -->
<?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="1024" minHeight="768" pageTitle="TheStudioOfCenyebao"
applicationComplete="init()">
<fx:Script>
<![CDATA[
// 改类会监视被绑定属性值的任何变化,如果有变化发生,该类就会触发必要的事件来监视这个值。
import mx.binding.utils.ChangeWatcher;
import mx.events.PropertyChangeEvent;
protected var _watcher:ChangeWatcher
protected function init():void{
toggleWatch();
}
// 切换是否监视变量;
protected function toggleWatch():void{
if(_watcher && _watcher.isWatching()) { // 判断监视器是否处于活动状态;
_watcher.unwatch(); // 移除监视的变量_关闭;
toggleButton.label = "startWatch";
} else { // 默认打开监视功能;
_watcher = ChangeWatcher.watch(inputfield, "text", onTextChange);
toggleButton.label = "stopWatching";
}
}
// textinput值改变后执行的函数;
protected function onTextChange(event:Event):void{
myLabel.text = inputfield.text;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 非可视元素 -->
</fx:Declarations>
<!--view-->
<s:VGroup horizontalAlign="right" horizontalCenter="0" verticalCenter="0">
<s:HGroup>
<s:TextInput id="inputfield" text="start text"/>
<s:Label id="myLabel" fontWeight="bold" fontSize="18"/>
</s:HGroup>
<s:Button id="toggleButton" label="WatchText" click="toggleWatch()"/>
</s:VGroup>
</s:Application>