2012-07-25 46 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

相关问题