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

发布于 2024-10-31 09:52:57 字数 55 浏览 3 评论 0原文

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

谢谢

How do I to make a function that replaces the selected text in text INPUT on the symbol of the selected one from each list?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-11-07 09:52:57

请尝试这个示例,它可能会有所帮助,谢谢

解释
如果您输入“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>

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>
画中仙 2024-11-07 09:52:57

此示例在单击列表时将 TextInput 中的选定文本替换为列表文本

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

嵌入 XML Flex 应用程序中的文件,使用

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

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

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

注意:程序中不需要进行其他更改

希望这会有所帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文