我知道如何从列表中拖动并放入textinput控件。但我不知道如何在textinput的光标位置下插入文本。例如,我在textinput中有文本qwerty
。我需要在textinput中删除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>
有没有办法实现这个目标?
答案 0 :(得分:2)
这是一个完整的例子。它遗憾地需要访问mx_internal命名空间,但应该保持良好状态。它必须使用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);"/>