隨筆 - 8  文章 - 55  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          朋友的Blog

          最新評論

          閱讀排行榜

          評論排行榜

          在MXML文件中實現ActionScript邏輯的幾種方法:
          最簡單的方法,在一個MXML文件中通過組件的事件直接書寫簡單的邏輯控制,但是并不推薦。

          <mx:Application xmlns:mx='http://www.macromedia.com/2003/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=(farenheit.text-32)/1.8;' />

          <mx:Label text='Temperature in Celsius:'/>

          <mx:Label id='celsius' width='120' fontSize='48'/>

          </mx:HBox>
          </mx:Panel>
          </mx:Application>

          第二種,在MXML文件中定義函數調用,比較適合簡單的應用,如

          <mx:Application xmlns:mx='http://www.macromedia.com/2003/mxml'>

          <mx:Script>

          <![CDATA[

          function calculate() {

          celsius.text=(farenheit.text-32)/1.8;

          }

          ]]>

          </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='120' fontSize='48'/>
          </mx:HBox>
          </mx:Panel>
          </mx:Application>

          第三種,把MXML文件和腳本文件分開,便于項目管理

          <mx:Application xmlns:mx='http://www.macromedia.com/2003/mxml'>

          <!-- Specify the ActionScript file containing the function. -->

          <mx:Script source='sample3script.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='120' fontSize='48'/>
          </mx:HBox>
          </mx:Panel>
          </mx:Application>

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

          第四種,使用MXML組件方式,更好的封裝實現。下面的例子定義了一個tempConverter組件

          <mx:Application xmlns:mx='http://www.macromedia.com/2003/mxml'

          initialize='converter.setupListener()'>

          <local:TempConverter id='converter' xmlns:local='*'/>

          <mx:Panel title='My Application' >

          <mx:HBox>

          <mx:Label text='Temperature in Farenheit:' />

          <mx:TextInput id='farenheit' width='120' />

          <mx:Button id='myButton' label='Convert' />

          <mx:Label text='Temperature in Celsius:' />
          <mx:Label id='celsius' width='120' fontSize='24' />
          </mx:HBox>
          </mx:Panel>
          </mx:Application>

          TempConverter.as文件代碼如下:

          class TempConverter implements mx.core.MXMLObject{

          public var view;

          function initialized(doc : Object, id : String) : Void {

          view = doc;

          }

          function setupListener() : Void {

          view.myButton.addEventListener('click', this);

          }

          function click(event) {
          view.celsius.text=(view.farenheit.text-32)/1.8;
          }
          }
          posted on 2006-04-29 13:40 blog搬家了--[www.ialway.com/blog] 閱讀(316) 評論(0)  編輯  收藏 所屬分類: Flex
          主站蜘蛛池模板: 黄浦区| 宜君县| 冷水江市| 翼城县| 宁乡县| 陈巴尔虎旗| 六安市| 什邡市| 阿克苏市| 郑州市| 珠海市| 武穴市| 金昌市| 汉阴县| 中卫市| 西宁市| 衡水市| 清涧县| 余庆县| 永福县| 江西省| 东城区| 泸定县| 林口县| 墨玉县| 鹰潭市| 潼关县| 奉化市| 陕西省| 木兰县| 榆林市| 黄骅市| 永丰县| 上林县| 邢台县| 洮南市| 祁门县| 南京市| 东丰县| 绥棱县| 牙克石市|