2012-07-25 45 views
0

我正在用mxml編寫一個非常簡單的flex應用程序。我有很多按鈕,當我點擊其中一個按鈕時,我希望它的值更改爲World有許多按鈕,當點擊其中一個按鈕時,如何更改其標籤?

我的代碼是:

<?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="955" minHeight="600"> 
    <fx:Declarations> 
    </fx:Declarations> 

    <fx:Script> 
     <![CDATA[ 
      private function hello():void { 
       this.label = "World!"; 
      } 
     ]]> 
    </fx:Script> 

    <mx:HBox> 
     <s:Button click="hello()" label="Hello" /> 
     <s:Button click="hello()" label="Hello" /> 
     <s:Button click="hello()" label="Hello" /> 
     <s:Button click="hello()" label="Hello" /> 
     <s:Button click="hello()" label="Hello" /> 
    </mx:HBox> 

</s:Application> 

這是不正確,因爲this.label = "World!"不能被編譯的this.label沒有找到。

如何讓this引用我點擊的按鈕,或者如何實現?

回答

1

試試下面這段代碼可以幫助你: -

<?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="955" minHeight="600"> 
    <fx:Declarations> 
    </fx:Declarations> 

    <fx:Script> 
     <![CDATA[ 
      private function hello(event:MouseEvent):void { 
       event.currentTarget.label = "World!"; 
      } 
     ]]> 
    </fx:Script> 

    <mx:HBox> 
     <s:Button click="hello(event)" label="Hello" /> 
     <s:Button click="hello(event)" label="Hello" /> 
     <s:Button click="hello(event)" label="Hello" /> 
     <s:Button click="hello(event)" label="Hello" /> 
     <s:Button click="hello(event)" label="Hello" /> 
    </mx:HBox> 

</s:Application> 
+0

謝謝,它的工作原理。我想知道「事件」是否唯一的方法?有沒有辦法將'this'這個「hello」函數綁定到一個按鈕上? – Freewind 2012-07-25 07:34:20

+0

不,這不是它在Flex中的工作方式,否則向反射當前按鈕'hello()hello(event,this)「的hello()函數添加一個參數,但它似乎沒用? – poussma 2012-07-25 07:46:43

+0

只是爲了澄清,在你的應用程序中調用'this'將引用'Application',而不是按鈕。 – AlBirdie 2012-07-25 07:58:19

相關問題