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

标签 actionscript-3 apache-flex flex4 flex4.6

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

最佳答案

只需使用 TextArea 的属性

selectionActivePosition

这是一个 working example .

//编辑

要将新字符串插入文本区域,请使用字符串函数,如 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>

关于actionscript-3 - 如何在flex4的textArea中找到当前光标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15964650/

相关文章:

apache-flex - 在 Flex 列表/数据网格中设置前景和背景选择颜色的样式

flash - AS3-声音提取到bytearray-进度处理程序

flash - AS3-从 1 个 BitmapData 对象(加载的 PNG)复制透明度并应用于另一个?

apache-flex - Flex - 自动调整数据网格大小的问题

flash - 我应该怎么做才能在 Flash Builder 中使用 .as3proj 文件?

apache-flex - Flex 4 自定义组件 - 如何通知皮肤属性更改?

ant - 我如何使用 ant build 在 Flash Builder 4 中执行 exportReleaseBuild 任务

ios - 在设备上调试 iOS/AIR 内容

arrays - AS3 使默认数组工作 : function example(human:Array = ["heart" ,"skull"])?

windows - adobe air app可以打包运行在windows 8上吗?