打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Flex精华摘要 4:使用AS脚本
在MXML文件中实现ActionScript逻辑的几种方法:
最简单的方法,在一个MXML文件中通过组件的事件直接书写简单的逻辑控制,但是并不推荐。
     
  1. <mx:Application xmlns:mx=‘http://www.macromedia.com/2003/mxml‘>
     
  2. <
  3. mx:Panel title=‘My Application‘ >
     
  4. <
  5. mx:HBox>
     
  6. <
  7. mx:Label text=‘Temperature in Farenheit:‘/>
     
  8. <
  9. mx:TextInput id=‘farenheit‘ width=‘120‘/>
     
  10. <
  11. mx:Button label=‘Convert‘ click=‘celsius.text=(farenheit.text-32)/1.8;‘ />
     
  12. <
  13. mx:Label text=‘Temperature in Celsius:‘/>
     
  14. <
  15. mx:Label id=‘celsius‘ width=‘120‘ fontSize=‘48‘/>
     
  16. </
  17. mx:HBox>
  18. </
  19. mx:Panel>
  20. </
  21. mx:Application>



第二种,在MXML文件中定义函数调用,比较适合简单的应用,如
     
  1. <mx:Application xmlns:mx=‘http://www.macromedia.com/2003/mxml‘>
     
  2. <
  3. mx:Script>
     
  4. <![
  5. CDATA[
     
  6. function 
  7. calculate() { 
     
  8. celsius.text=(farenheit.text-32)/1.8;
     
  9.  }
     
  10. ]]>
     
  11. </
  12. mx:Script>
     
  13. <
  14. mx:Panel title=‘My Application‘ >
  15. <
  16. mx:HBox>
  17. <
  18. mx:Label text=‘Temperature in Farenheit:‘/>
  19. <
  20. mx:TextInput id=‘farenheit‘ width=‘120‘/>
  21. <
  22. mx:Button label=‘Convert‘ click=‘calculate();‘ />
  23. <
  24. mx:Label text=‘Temperature in Celsius:‘/>
  25. <
  26. mx:Label id=‘celsius‘ width=‘120‘ fontSize=‘48‘/>
  27. </
  28. mx:HBox>
  29. </
  30. mx:Panel>
  31. </
  32. mx:Application>



第三种,把MXML文件和脚本文件分开,便于项目管理
     
  1. <mx:Application xmlns:mx=‘http://www.macromedia.com/2003/mxml‘>
     
  2. <!-- 
  3. Specify the ActionScript file containing the function. -->
     
  4. <
  5. mx:Script source=‘sample3script.as‘/>
     
  6. <
  7. mx:Panel title=‘My Application‘ >
     
  8. <
  9. mx:HBox>
     
  10. <
  11. mx:Label text=‘Temperature in Farenheit:‘/>
     
  12. <
  13. mx:TextInput id=‘farenheit‘ width=‘120‘/>
     
  14. <
  15. mx:Button label=‘Convert‘ click=‘calculate();‘ />
     
  16. <
  17. mx:Label text=‘Temperature in Celsius:‘/>
  18. <
  19. mx:Label id=‘celsius‘ width=‘120‘ fontSize=‘48‘/>
  20. </
  21. mx:HBox>
  22. </
  23. mx:Panel>
  24. </
  25. mx:Application>


sample.as文件代码如下:
  1. function calculate() { 
  2. celsius.text=(farenheit.text-32)/1.8;
  3.  }



第四种,使用MXML组件方式,更好的封装实现。下面的例子定义了一个tempConverter组件
     
  1. <mx:Application xmlns:mx=‘http://www.macromedia.com/2003/mxml‘
     
  2. initialize=‘converter.setupListener()‘>
     
  3. <
  4. local:TempConverter id=‘converter‘ xmlns:local=‘*‘/>
     
  5. <
  6. mx:Panel title=‘My Application‘ >
     
  7. <
  8. mx:HBox>
     
  9. <
  10. mx:Label text=‘Temperature in Farenheit:‘ />
     
  11. <
  12. mx:TextInput id=‘farenheit‘ width=‘120‘ />
     
  13. <
  14. mx:Button id=‘myButton‘ label=‘Convert‘ />
     
  15. <
  16. mx:Label text=‘Temperature in Celsius:‘ />
  17. <
  18. mx:Label id=‘celsius‘ width=‘120‘ fontSize=‘24‘ />
  19. </
  20. mx:HBox>
  21. </
  22. mx:Panel>
  23. </
  24. mx:Application>


TempConverter.as文件代码如下:
     
  1. class TempConverter implements mx.core.MXMLObject
     
  2. public var view;
     
  3. function 
  4. initialized(doc Objectid String) : Void 
     
  5. view doc;
     
  6.  }
     
  7. function 
  8. setupListener() : Void 
     
  9. view.myButton.addEventListener(‘click‘this);
     
  10.  }
     
  11. function 
  12. click(event) { 
  13. view.celsius.text=(view.farenheit.text-32)/1.8;
  14.  }
  15.  }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
理解 Flex itemRenderer - 第 1 部分: 内联渲染器 - czx338...
Flex itemRenderer
Flex 入门教程
Flex data binding pitfalls: common misuses and mistakes | Adobe Developer Connection
InfoQ: Flex与JSON及XML的互操作
Flex之拖放
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服