如何获取文档中某种类型的所有元素的列表?

时间:2015-07-26 11:18:53

标签: actionscript-3 flex flex4

我有一个MXML组件,我想获得一个特定类型的所有元素的数组,但我不认为这个功能是内置于Flex中的。

例如,我想在文档中获取所有“Label”和所有“HGroup”组件。

1 个答案:

答案 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;
    }