问题描述
|
我是Flex 3和ActionScript的新手。
我想知道如何通过其ID获得动态文本框的值。
for (var countz:int = 0; countz < questionCount; countz++)
{
hBoxtextBoxz = new HBox();
txt = new Textinput();
txt.id = countz + \"\";
hBoxtextBoxz.addChild(txt);
}
有谁知道如何从使用for循环创建的动态文本框中获取值?
解决方法
要动态获取容器的所有子代,请使用
getChildren()
方法。它将返回一个数组UIComponent
,如果它们是TextInput
实例,则将其强制转换并使用text
属性获取值。
从HBoxes内部的容器中获取所有textBox的示例代码。
var children:ArrayCollection = textBoxContainer.getChildren();
for(var i:int = 0; i < children.length; i++)
{
var hbox:HBox = HBox(children[i]);
var textBox:TextInput = TextInput( hbox.getChildAt(0));
if(textBox != null)
{
trace(textBox.text);
}
}
提供以上代码,您的UI结构如下:
<VBox id=\"textBoxContainer\">
<HBox>
<TextInput/>
</HBox>
....
</VBox>
,用值表示框内的文字吗?
如果要在for循环中访问它,只需使用变量名:
txt.text
否则,如果您在MXML中创建文本框,则可以设置其id参数并使用该参数访问它:
(文字栏位编号).text
应该为您提供已插入该框中的文本。