Flex:更新标签的文本,这是一个变量 [英] Flex: Update a label's text, which is a variable
问题描述
我有一个标签,当你点击一个按钮时,它会从var中获取它的值。 var已被声明:
public function clickevent
{
label.text = aVariable;
$ b现在我知道如果我有这样的标签: < s:Label id =label2text ={aVariable}/>
和aVariable是空的,label2的文本是Null(它不会给出错误,只是Null 在我的情况)。这是我目前的情况。
我想知道的是当我稍后将aVariable的值更改为字符串hasChanged时,例如。标签2的文本也应该改变为hasChanged,而用户不必按下按钮或任何东西来进行这种改变。如何做到这一点?
解决方案我不是100%确定我理解你的问题,但如果你的变量被声明为可绑定,无论你的脚本改变了它的值还是一个按钮,标签的文本属性都会随着绑定而变化。
<?xml version =1.0encoding =utf-8?>
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx>
< fx:Script>
<![CDATA [
[Bindable]
private var aVariable:String;
protected function button1_clickHandler(event:MouseEvent):void
{
aVariable =My new value;
}
]]>
< / fx:Script>
< s:layout>
< s:VerticalLayout />
< / s:layout>
< / s:WindowedApplication>
I have a label that get it's value from a var when you click on a button. The var has already been declared:
public function clickevent
{
label.text = aVariable;
}
Now I know that if i have a label like this:
<s:Label id="label2" text="{aVariable}"/>
and aVariable is empty, label2's text is Null (it doesn't give an error, just "Null" in my situation). This is my current situation.
What I'd like to know is when I later on change the aVariable's value to a string "hasChanged", for example. The label2's text should also change to "hasChanged" without the user having to push a button or anything to make this change. How can this be done?
解决方案 I'm not 100% sure I understand your question but if your variable is declared as "bindable", no matter if your script change its value or a button, your text propertie of the label will follow as it is binded.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
[Bindable]
private var aVariable:String;
protected function button1_clickHandler(event:MouseEvent):void
{
aVariable = "My new value";
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label text="{aVariable}"/>
<s:Button label="Click me" click="button1_clickHandler(event)"/>
</s:WindowedApplication>
这篇关于Flex:更新标签的文本,这是一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!