您的位置:首页 > 其它

Flex精华摘要--使用AS脚本

2009-08-12 22:38 357 查看
在MXML文件中实现ActionScript逻辑的几种方法:
最简单的方法,在一个MXML文件中通过组件的事件直接书写简单的逻辑控制,但是并不推荐。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel title='My Application' >
<mx:HBox>
<mx:Label text='Temperature in Farenheit:'/>
<mx:TextInput id='farenheit' width='120'/>
<mx:Button label='Convert' click='celsius.text=((int(farenheit.text)-32)/1.8).toString();' />
<mx:Label text='Temperature in Celsius:'/>
<mx:Label id='celsius' width='200' fontSize='48'/>
</mx:HBox>
</mx:Panel>
</mx:Application>


注意其中的类型转换

第二种,在MXML文件中定义函数调用,比较适合简单的应用,如

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<!--[CDATA[
public function calculate():void {
celsius.text=((int(farenheit.text)-32)/1.8).toString();
}
]]-->
</mx:Script>
<mx:Panel title='My Application' >
<mx:HBox>
<mx:Label text='Temperature in Farenheit:'/>
<mx:TextInput id='farenheit' width='120'/>
<mx:Button label='Convert' click='calculate()' />
<mx:Label text='Temperature in Celsius:'/>
<mx:Label id='celsius' width='200' fontSize='48'/>
</mx:HBox>
</mx:Panel>
</mx:Application>


第三种,把MXML文件和脚本文件分开,便于项目管理

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script source="sample.as"/>
<mx:Panel title='My Application' >
<mx:HBox>
<mx:Label text='Temperature in Farenheit:'/>
<mx:TextInput id='farenheit' width='120'/>
<mx:Button label='Convert' click='calculate()' />
<mx:Label text='Temperature in Celsius:'/>
<mx:Label id='celsius' width='200' fontSize='48'/>
</mx:HBox>
</mx:Panel>
</mx:Application>

sample.as

public function calculate():void {
celsius.text=((int(farenheit.text)-32)/1.8).toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: