【问题标题】:Flex combobox child (textInput, button) accessFlex 组合框子(textInput,按钮)访问
【发布时间】:2011-12-15 16:53:10
【问题描述】:

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

【问题讨论】:

    标签: actionscript-3 apache-flex adobe


    【解决方案1】:

    您可以像这样向组合框的 textInput 添加一个事件:

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

    因为 textInput 对象是组合框对象 (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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多