简体   繁体   中英

How to campture the text change event of a text control to call a function in Flex 3

I am very new to flex and have trouble capturing the text change event of my text control:

<mx:Text id="description" 
             text="" 
             textAlign="center" 
             fontSize="18" 
             click="_playSpeech()"   />

it's a click right now but i need it to be something like textChange="_playSpeech()" so it calls that function every time the text of the of that control changes.

Any help is highly appreciated.

An easy way to do it within the code is by wrapping the text update into a function:

        protected function updateDescription(text:String):void
        {
            description.text=text;
            _playSearch();
        }

and then always call the function instead of immediately change the text field. Also prevents unnecessary use of events.

If you are watching changes that the user enters in the textbox itself you should use the class TextArea or TextInput instead of Text . This allows you to listen to changes like this:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[


            protected function onTextChangeHandler(event:Event):void
            {
                trace("text has changed")
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>


    <mx:TextArea id="description" 
             text="text to change goes here ..." 
             textAlign="center" 
             fontSize="18" 
             change="onTextChangeHandler(event)"   />

</s:Application>

If you are changing the text outside the textbox I would recommend you to make a bindable variable or a function that triggers the action you want to happen.

您将需要像这样注册更改事件(注意,我使用的是TextInput而不是Text):

<mx:TextInput id="description" text="" fontSize="18" change="_playSpeach()"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM