如何在flex4中的textArea中找到当前光标位置?

时间:2013-04-12 06:34:56

标签: actionscript-3 flex flex4 flex4.6

我想用键盘(内置键盘)在文本区域写一些内容,并希望在当前光标位置添加由我制作的键盘中的其他内容。

1 个答案:

答案 0 :(得分:1)

只需使用TextArea的属性

即可
selectionActivePosition

这是working example

//修改

要在textarea中插入新字符串,请使用字符串函数,如substr()和length()。插入后,您应该通过添加插入字符串的长度来更改光标的当前位置。

修改//

这是我的代码:

<?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 onBtnInsert(event:MouseEvent):void
        {
            var str:String = "[new text]";

            var pos:int = taMain.selectionActivePosition;

            if (pos != -1)
            {
                taMain.text = taMain.text.substr(0, pos) + str + taMain.text.substr(pos, taMain.text.length - pos);
                taMain.selectRange(pos + str.length, pos + str.length);
            }
        }
    ]]>
</fx:Script>

<s:VGroup x="20" y="20">
    <s:TextArea id="taMain" width="200" height="150"  text="I want to write something in text area with keyboard(built in keyboard) and want to add something other from the keyboard made by me at the current cursor position."/>
    <s:HGroup verticalAlign="bottom">
        <s:Button label="Get Pos" click="{laPos.text = taMain.selectionActivePosition.toString()}"/>
        <s:Label text="Current position: "/>
        <s:Label id="laPos"/>
    </s:HGroup>

    <s:Button label="Insert text" click="onBtnInsert(event)"/>
</s:VGroup>
</s:Application>