Flex 文本控件实现自定义复制粘贴

由于添加了自定义右键菜单,导致Textinput控件默认的右键复制粘贴功能被屏蔽了。最后通过JS脚本实现这个功能,参考代码如下

 <?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" creationComplete="init()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<fx:XML id="jsxml" >
<script>
<![CDATA[ function clipboardOperator(type,browsername,txt){
if(type!="init"){
if(type=="copy"){
window.clipboardData.setData("Text", txt);
return;
}
else if(type=="paste"){
var clipboard=window.clipboardData.getData("Text");
if(clipboard == null){
alert('您的剪切板中没有任何文本内容');
return ;
}
else
return clipboard;
}
}
}
]]>
</script>
</fx:XML>
</fx:Declarations>
<fx:Script>
<![CDATA[
import Class.URLUtil; import flash.external.ExternalInterface; import flash.desktop.Clipboard;
import flash.desktop.ClipboardFormats;
import mx.controls.Alert; private var browsername:String=URLUtil.getBrowserName(); private function init():void{ lbpaste.text=ExternalInterface.call(jsxml, "init");
} protected function btncopy_clickHandler(event:MouseEvent):void
{
//内部复制功能
Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT,txt.text,false);
//调用JS脚本实现复制
// ExternalInterface.call("copypasteOperator","copy", txt.text);
} protected function btnpaste_clickHandler(event:MouseEvent):void
{
lbpaste.text=ExternalInterface.call("clipboardOperator","paste",browsername);
}
]]>
</fx:Script>
<s:TextInput id="txt" text="aaa"/>
<s:Button id="btncopy" label="复制" click="btncopy_clickHandler(event)"/>
<s:Button id="btnpaste" label="粘贴" click="btnpaste_clickHandler(event)"/>
<s:Label id="lbpaste"/>
</s:Application>

注:目前只支持IE和搜狗浏览器

上一篇:js 复制粘贴功能记录


下一篇:如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图