开发者

Action Script - how to access label component defined in mxml file within action script class method?

I have a label component in a mxml file like below

<mx:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>

if i want to access it and change its text content within a method defined in action script class 开发者_JS百科that i have written, how to do it?

lble.text="test";


To access the label, you have to import the Label component before the class definition, so it can be accessed:

import mx.controls.Label;

Then, declare the reference to the label in your class body:

public var lble:Label;

And, finally, you can address the label to manipulate it:

lble.text = "Hello world!";


The ID attribute makes it a private variable within the class or component, so

lble.text = "test";

is just fine.

You are talking about doing this within the same component or class, right? If not you should bind the value to a variable and use getters and setters, like so

[Bindable]
private var _labelText:String;

public function get labelText() : String {
  return _labelText;
}

public function set labelText(value:String) : void {
  _labelText = value;
}

and then

<mx:Label text="{_labelText}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜