flex实现在TextArea的光标位置插入字符2009年04月09日 星期四 下午 12:56<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" viewSourceURL="srcview/index.html" fontSize="12">
<mx:Script>
<![CDATA[
//doubleClickEnabled属性:指定对象是否接收 doubleClick 事件。默认值为 false,这意味着在默认情况下,不接收 doubleClick 事件。
//如果将 doubleClickEnabled 属性设置为 true,实例在其范围内接收 doubleClick 事件
private function doubleClickHandler(evt:MouseEvent):void
{
navigateToURL(new URLRequest("http://hi.baidu.com/woaidelphi/"), "_blank");
}
private function insertString(insertStr:String):void
{
//如果选择文本的话,执行下面的
if (this.textEditor.selectionBeginIndex == this.textEditor.selectionEndIndex)
{
var startPart:String=this.textEditor.text.substring(0, this.textEditor.selectionBeginIndex);
var endPart:String=this.textEditor.text.substring(this.textEditor.selectionEndIndex, this.textEditor.text.length);
startPart+=insertStr;
startPart+=endPart;
this.textEditor.text=startPart;
}
else//否则
{
this.textEditor.text=insertStr;
}
}
]]>
</mx:Script>
<mx:Button doubleClickEnabled="true" doubleClick="doubleClickHandler(event)" label="双击我,同共奋斗!" width="143" height="38"/>
<mx:TextArea id="textEditor" x="11" y="366" width="399"/>
<mx:Button label="单击它,TextArea的光标位置插入字符" click="insertString('有空要访问我的空间啊。')"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" viewSourceURL="srcview/index.html" fontSize="12">
<mx:Script>
<![CDATA[
//doubleClickEnabled属性:指定对象是否接收 doubleClick 事件。默认值为 false,这意味着在默认情况下,不接收 doubleClick 事件。
//如果将 doubleClickEnabled 属性设置为 true,实例在其范围内接收 doubleClick 事件
private function doubleClickHandler(evt:MouseEvent):void
{
navigateToURL(new URLRequest("http://hi.baidu.com/woaidelphi/"), "_blank");
}
private function insertString(insertStr:String):void
{
//如果选择文本的话,执行下面的
if (this.textEditor.selectionBeginIndex == this.textEditor.selectionEndIndex)
{
var startPart:String=this.textEditor.text.substring(0, this.textEditor.selectionBeginIndex);
var endPart:String=this.textEditor.text.substring(this.textEditor.selectionEndIndex, this.textEditor.text.length);
startPart+=insertStr;
startPart+=endPart;
this.textEditor.text=startPart;
}
else//否则
{
this.textEditor.text=insertStr;
}
}
]]>
</mx:Script>
<mx:Button doubleClickEnabled="true" doubleClick="doubleClickHandler(event)" label="双击我,同共奋斗!" width="143" height="38"/>
<mx:TextArea id="textEditor" x="11" y="366" width="399"/>
<mx:Button label="单击它,TextArea的光标位置插入字符" click="insertString('有空要访问我的空间啊。')"/>
</mx:Application>