How do I to make a function that repl开发者_如何学JAVAaces the selected text in text INPUT on the symbol of the selected one from each list?
Thank you
Please try this sample it may be help full thanks
Explaination If you type "Imran $" it will replace it with "Imran Hello" on text change Event you may also use any other event
Code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var lst:Array = new Array(
{msg:"Hello", data:"$"},
{msg:"Bye", data:"@"}
);
private function textchanged(event:Event):void
{
var len:int = txt.text.length;
if(len!=0)
{
var msg:String = getMessage(txt.text.charAt(len-1));
if(msg!= null)
{
txt.removeEventListener(Event.CHANGE,textchanged);
txt.addEventListener(FlexEvent.UPDATE_COMPLETE,updateComplete);
txt.text = txt.text.slice(0, len-1) + " "+ msg;
}
}
}
private function updateComplete(event:FlexEvent):void
{
txt.addEventListener(Event.CHANGE,textchanged);
}
private function getMessage(data:String):String
{
var msg:String = null;
for each(var obj:Object in lst)
{
if(obj.data == data)
{
msg = obj.msg;
break;
}
}
return msg;
}
]]>
</mx:Script>
<mx:TextInput id="txt" change="textchanged(event)" />
</mx:Application>
This Sampe Replace selected text in TextInput by List Text on Clicking List
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Bindable]
private var arr:Array = new Array(
{msg:"Hello", data:"$"},
{msg:"Bye", data:"@"}
);
private function replaceSelection(event:MouseEvent):void
{
var obj:Object = lst.selectedItem;
var selStr:int = txt.selectionBeginIndex;
var selEnd:int = txt.selectionEndIndex;
if((lst.selectedItem != null)&&(selStr!=selEnd))
{
txt.text = txt.text.substring(0,selStr)+lst.selectedItem.msg.toString()+txt.text.substring(selEnd,txt.text.length)
}
}
]]>
</mx:Script>
<mx:VBox>
<mx:TextInput id="txt" />
<mx:List id="lst" dataProvider="{arr}" labelField="msg" click="{replaceSelection(event)}"/>
</mx:VBox>
</mx:Application>
EDITED: Modification needed Embed XML File as data source
XML File "messages.xml" {Its in same/root folder of Flex Source}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item><msg>Hello</msg></item>
<item><msg>Bye</msg></item>
</root>
Embeding XML File in Flex Application, using MXML component
<mx:XML id="xml" source="messages.xml" />
Modification in List Control, for working with XML
<mx:List id="lst" dataProvider="{xml.item}" labelField="msg" click="{replaceSelection(event)}"/>
Note: No other changes required in Program
Hopefully this will help
精彩评论