2011-12-15 45 views
0

我需要訪問組合框子(textinput和按鈕)而不創建自定義組件。我知道最好的做法是創建一個自定義組件,但仍然需要訪問像textinput一樣的組合框子並監聽它們的事件。任何幫助?Flex組合框子(textInput,按鈕)訪問

回答

0

您可以將事件添加到您的ComboBox控件的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> 
0

您可以爲Event.ADDED事件偵聽器添加到組合框和檢查event.target的類型針點所需的顯示對象(例如if (event.target is TextField) doStuff();),您將無法訪問該組合框的財產(以取代文本字段或不同的按鈕),但您可以修改已添加到舞臺的實例。因爲TextInput對象是ComboBox對象(myComboBox.textInput)的孩子

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