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.
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.
You'll want to register on the change event like so (Notice I'm using TextInput and not Text):
<mx:TextInput id="description" text="" fontSize="18" change="_playSpeach()"/>
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.
精彩评论