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

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

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" 
 4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">
 5     <s:layout>
 6         <s:VerticalLayout/>
 7     </s:layout>
 8     <fx:Declarations>
 9         <!-- 将非可视元素(例如服务、值对象)放在此处 -->
10         <fx:XML id="jsxml" >
11             <script>
12                 <![CDATA[
13                 
14                     function clipboardOperator(type,browsername,txt){
15                         if(type!="init"){
16                             if(type=="copy"){
17                                 window.clipboardData.setData("Text", txt);
18                                 return;
19                             }
20                             else if(type=="paste"){
21                                 var clipboard=window.clipboardData.getData("Text"); 
22                                 if(clipboard == null){
23                                     alert('您的剪切板中没有任何文本内容');
24                                     return ;
25                                 }
26                                 else
27                                     return clipboard;        
28                             }
29                         }
30                     }
31                 ]]>
32             </script>
33         </fx:XML>
34     </fx:Declarations>
35     <fx:Script>
36         <![CDATA[
37             import Class.URLUtil;
38             
39             import flash.external.ExternalInterface;
40             
41             import flash.desktop.Clipboard;
42             import flash.desktop.ClipboardFormats;
43             import mx.controls.Alert;
44             
45             private var browsername:String=URLUtil.getBrowserName();
46             
47             private function init():void{
48                 
49                 lbpaste.text=ExternalInterface.call(jsxml, "init"); 
50             }
51             
52             protected function btncopy_clickHandler(event:MouseEvent):void
53             {
54                 //内部复制功能
55                 Clipboard.generalClipboard.clear();
56                 Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT,txt.text,false);
57                 //调用JS脚本实现复制
58 //                ExternalInterface.call("copypasteOperator","copy", txt.text);
59             } 
60             
61             protected function btnpaste_clickHandler(event:MouseEvent):void
62             {
63                 lbpaste.text=ExternalInterface.call("clipboardOperator","paste",browsername);
64             }
65         ]]>
66     </fx:Script>
67     <s:TextInput id="txt" text="aaa"/>
68     <s:Button id="btncopy" label="复制" click="btncopy_clickHandler(event)"/>
69     <s:Button id="btnpaste" label="粘贴" click="btnpaste_clickHandler(event)"/>
70     <s:Label id="lbpaste"/>
71 </s:Application>

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

posted @ 2013-11-14 14:48  Anlycp  阅读(848)  评论(0编辑  收藏  举报