Apache flex 如何将列表中的文本放到特定的文本输入位置(光标下)?
我知道如何从列表中拖放到textinput控件中。但我不知道如何在textinput中的光标位置下插入文本。例如,我在textinput中有textApache flex 如何将列表中的文本放到特定的文本输入位置(光标下)?,apache-flex,actionscript-3,flash-builder,Apache Flex,Actionscript 3,Flash Builder,我知道如何从列表中拖放到textinput控件中。但我不知道如何在textinput中的光标位置下插入文本。例如,我在textinput中有textqwerty。我需要在文本输入中删除单词asdf。因此,我希望获得文本qweasdfrty或qasdfwerty,或者根据光标位置获得我想要的任何内容 以下是我已有的简化代码: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:fx="http://ns.ado
qwerty
。我需要在文本输入中删除单词asdf
。因此,我希望获得文本qweasdfrty
或qasdfwerty
,或者根据光标位置获得我想要的任何内容
以下是我已有的简化代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
layout="absolute"
minWidth="955"
minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private function init():void
{
horList.dataProvider=new ArrayCollection(["Reading", "Television", "Movies"]);
}
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.events.DragEvent;
import flash.events.MouseEvent;
private function dragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
DragManager.acceptDragDrop(TextInput(event.currentTarget));
}
private function dragOverHandler(event:DragEvent):void
{
if (event.dragSource.hasFormat("items"))
DragManager.showFeedback(DragManager.COPY);
}
private function dragDropHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
{
var draggedText:Array = event.dragSource.dataForFormat("items") as Array;
var textInput : TextInput = TextInput(event.currentTarget);
// here i want to insert the text from (draggedText[0] as String) into textInput
}
}
]]>
</fx:Script>
<mx:HorizontalList id="horList"
x="10"
y="10"
width="625"
dragEnabled="true"
creationComplete="init()">
</mx:HorizontalList>
<mx:TextInput id="destTextInput"
x="100"
y="117"
dragEnter="dragEnterHandler(event);"
dragOver="dragOverHandler(event);"
dragDrop="dragDropHandler(event);"/>
</mx:Application>
有什么方法可以做到这一点吗?下面是一个完整的示例。不幸的是,它需要访问mx_内部名称空间,但应该可以正常运行。它必须使用mx_internal的原因是为了访问IUITextField引用
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.core.IUITextField;
//Note use of mx_internal namespace
import mx.core.mx_internal;
use namespace mx_internal;
private function init():void
{
horList.dataProvider=new ArrayCollection(["Reading", "Television", "Movies"]);
}
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.events.DragEvent;
import flash.events.MouseEvent;
private function dragEnterHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
DragManager.acceptDragDrop(TextInput(event.currentTarget));
}
private function dragOverHandler(event:DragEvent):void
{
if (event.dragSource.hasFormat("items"))
DragManager.showFeedback(DragManager.COPY);
}
private function dragDropHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("items"))
{
var draggedText:Array = event.dragSource.dataForFormat("items") as Array;
var textInput : TextInput = TextInput(event.currentTarget);
// here i want to insert the text from (draggedText[0] as String) into textInput
//Using mx_internal namespace to gain access to internal textfield
var tf:IUITextField = textInput.getTextField();
var charIndex:int = tf.getCharIndexAtPoint(textInput.contentMouseX, textInput.contentMouseY);
//dropped after end of text
if(charIndex == -1 && mouseX > tf.textWidth)
{
tf.appendText(draggedText[0]);
}
//Dropped at beginning of field (only happens when it is empty)
else if(charIndex == -1 && mouseX < tf.textWidth)
{
tf.text = draggedText[0];
}
//dropped inline to text
else
{
tf.replaceText(charIndex, charIndex, draggedText[0]);
}
}
}
]]>
</fx:Script>
<mx:HorizontalList id="horList"
x="10"
y="10"
width="625"
dragEnabled="true"
creationComplete="init()">
</mx:HorizontalList>
<mx:TextInput id="destTextInput"
x="100"
y="117"
dragEnter="dragEnterHandler(event);"
dragOver="dragOverHandler(event);"
dragDrop="dragDropHandler(event);"/>
tf.textWidth)
{
tf.appendText(draggedText[0]);
}
//在字段开头删除(仅在字段为空时发生)
else if(charIndex==-1&&mouseX
我已经在TextLineMetrics类的帮助下解决了这个问题。但是谢谢你,你的变体更简单更好。但是当textWidth大于textInput.width并且部分文本被隐藏时会出现问题。在这种情况下,char索引是错误的。