柔性。替换文本输入中的选定文本

时间:2011-04-11 08:11:20

标签: flash flex actionscript-3 textinput

如何在每个列表中创建一个替换所选文本INPUT中所选文本的函数?

谢谢

2 个答案:

答案 0 :(得分:1)

请尝试此示例,感谢您的帮助

<强>释 如果您输入“Imran $”,它将在文本更改事件中将其替换为“Imran Hello”,您也可以使用任何其他事件

<强>代码

 <?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>

答案 1 :(得分:1)

此Sampe将文本输入中的选定文本替换为单击列表上的列表文本

<?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>

已编辑:修改需要将XML文件嵌入为数据源

XML文件“messages.xml”{它位于Flex Source的同一/根文件夹中}

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item><msg>Hello</msg></item>
    <item><msg>Bye</msg></item>
</root>

使用MXML组件在Flex应用程序中嵌入XML文件

<mx:XML id="xml" source="messages.xml" />

列表控件中的修改,用于处理XML

<mx:List id="lst" dataProvider="{xml.item}" labelField="msg" click="{replaceSelection(event)}"/>

注意:计划

中无需其他更改

希望这会有所帮助