我有一个MXML组件,我想获得一个特定类型的所有元素的数组,但我不认为这个功能是内置于Flex中的。
例如,我想在文档中获取所有“Label”和所有“HGroup”组件。
答案 0 :(得分:0)
这似乎可以解决问题:
/**
* Gets the elements by type
* */
public function getElementsByType(container:IVisualElementContainer, type:Class, elements:Array = null):Array {
if (elements==null) elements = [];
for (var i:int;i<container.numElements;i++) {
var element:IVisualElement = container.getElementAt(i);
if (element is type) {
elements.push(element);
}
if (element is IVisualElementContainer) {
getElementsByType(element as IVisualElementContainer, type, elements);
}
}
return elements;
}