Flex组合框子(textInput,button)访问

时间:2011-12-15 16:53:11

标签: actionscript-3 flex adobe

我需要访问组合框子项(textinput和button)而不创建自定义组件。我知道最好的做法是创建一个自定义组件,但仍然需要像textinput一样访问组合框子项并监听他们的事件。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

您可以将Event.ADDED的事件侦听器添加到ComboBox并检查event.target的类型以指向所需的显示对象(例如if (event.target is TextField ) doStuff();)您将无法访问组合框'属性(用另一个替换文本字段或按钮),但您可以修改添加到舞台的实例。

答案 1 :(得分:0)

您可以将事件添加到comboBox的textInput,如下所示:

myComboBox.textInput.addEventListener(TextOperationEvent.CHANGE, myFunction);

因为textInput对象是comboBox对象的子对象(myComboBox.textInput)。


这是一个完整的工作示例

<?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" creationComplete="creationCompleteHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            import spark.events.IndexChangeEvent;
            import spark.events.TextOperationEvent;

            [Bindable]
            private var _dp:ArrayCollection = new ArrayCollection([
                {id : "1", name : "Paul"},
                {id : "2", name : "Andrew"},
                {id : "2", name : "Bob"}
            ]);

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                myComboBox.textInput.addEventListener(TextOperationEvent.CHANGE, showTextInputValue);
                myComboBox.addEventListener(IndexChangeEvent.CHANGE, showComboValue);
            }

            protected function showTextInputValue(event:TextOperationEvent):void
            {
                textFieldValue.text = "myComboBox.textInput : " + event.currentTarget.text;
            }

            protected function showComboValue(event:IndexChangeEvent):void
            {
                if (event.newIndex > -1)
                    comboBoxValue.text = "myComboBox selected item is : " + myComboBox.selectedItem.name;
            }

        ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <s:ComboBox id="myComboBox" labelField="name" dataProvider="{_dp}"/>

    <mx:Spacer height="100"/>

    <s:Label id="textFieldValue"/>

    <s:Label id="comboBoxValue"/>

</s:WindowedApplication>