4

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

4

1 回答 1

1

只需使用 TextArea 的属性

selectionActivePosition

这是一个工作示例

//编辑

要将新字符串插入文本区域,请使用字符串函数,如 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>
于 2013-04-13T15:09:50.903 回答