我需要在不创建自定义组件的情况下访问组合框子组件(textinput和button)。我知道最佳实践是创建自定义组件,但仍然需要访问组合框子组件(如textinput)并侦听它们的事件。有什么帮助吗?
你可以像这样给你的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>
你可以为Event.ADDED
添加一个事件监听器到ComboBox,并检查event.target
的类型以pin point所需的显示对象(例如if (event.target is TextField ) doStuff();
)。你将无法访问ComboBox的属性(以替换文本字段或按钮与不同的),但你可以修改添加到舞台的实例