AIR项目:一个选词小工具

效果:先选择要读取的本地txt文件,然后选择文档修改后的保存路径。下面就可以就行选词修改了。每次只修改txt文档中的一行。记住,一定要让鼠标从左往右划词,不能双击划词或者从右往左,这个问题还没解决。当选择词语的时候,下面的Textarea就会显示选择的词语。当点击“Next”时。刚刚修改的那一行文本将被以xml文件的形式保存在刚刚指定的路径中。并且下面的Textarea会显示选词之后整个句子的结果。每次点击“Next”,都会把数据重新更新到xml文件中。

代码部分:

  1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 * ComingX.com Business License
4 *
5 * Copyright 2012. All rights reserved.
6 *
7 * @Author: ShanLanjie
8 * @Email: shanlanjie#gmail.com
9 * @Created date: 2012-3-23
10 -->
11 <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
12 xmlns:s="library://ns.adobe.com/flex/spark"
13 xmlns:mx="library://ns.adobe.com/flex/mx"
14 creationComplete="windowedapplication1_creationCompleteHandler(event)"
15 >
16 <fx:Script>
17 <![CDATA[
18
19 import mx.collections.ArrayCollection;
20 import mx.events.FlexEvent;
21
22 [Bindable]
23 public var textList:ArrayCollection=new ArrayCollection();
24
25 public var filestream:FileStream=new FileStream();
26 public var index:int=0;
27 public var mouseDown:Boolean=false;
28 public var beginIndex:int; //记录开始选择的索引
29 public var endIndex:int; //记录选择结束的索引
30 public var chooseStr:String; //记录选择的词语
31 public var chooseAll:String=""; //记录把所有选择过的词语
32 public var resultStr:String=""; //记录选择所剩结果
33 public var file2:File;
34
35 protected function button1_clickHandler(event:MouseEvent):void
36 {
37 var file:File=File.documentsDirectory;
38 var openFilter:FileFilter=new FileFilter("文档文件","*.txt;*.xml"); //过滤文件
39 file.browseForOpen("请打开选择的文件",[openFilter]);
40 file.addEventListener(Event.SELECT,onSelectHandler);
41 }
42 //将文件写入文本框
43 public function onSelectHandler(evt:Event):void
44 {
45 var file:File=evt.target as File;
46 var fileStream:FileStream=new FileStream();
47 fileStream.open(file,FileMode.READ); //只读形式
48
49 var textField:TextField=new TextField();
50 textField.text=fileStream.readMultiByte(fileStream.bytesAvailable, "gb2312");//j解决读取时中文乱码问题
51 textField.condenseWhite=true;
52 //遍历整个文本,把每行的数据存放到Arraycollention中
53 for (var i:int=0;i<textField.numLines;++i)
54 {
55 if(textField.getLineLength(i)>1)
56 {
57 textList.addItem(textField.getLineText(i)); //把文档的每一行保存到Arraycollention中
58 }
59 }
60 fileStream.close();
61 note.text=( textList.getItemAt(0) ).toString();
62 resultStr=note.text;
63 choose.enabled=false;
64 savePath.enabled=true;
65 }
66
67 public function button2_clickHandler(evt:MouseEvent):void
68 {
69 result.appendText(resultStr+"\n");
70 result.appendText("--------------------------------------------------------------------------------"+"\n");
71
72 //把数据保存到本地
73 // filestream.writeUTFBytes(result.text);
74 filestream.open(file2,FileMode.WRITE);
75 filestream.writeMultiByte(result.text,"gb2312");
76 // result.text="";
77
78 index++;
79 if( index<=textList.length-1)
80 {
81 note.text=( textList.getItemAt( index ) ).toString();
82 }
83 else
84 {
85 next.enabled=false;
86 next.label="已经是最后了";
87 }
88 resultStr=note.text; //重新给resultStr赋值
89 chooseAll="";
90 }
91
92 protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
93 {
94 note.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
95 }
96
97 /* 当鼠标按下时记录第一个字符索引值 */
98 public function onMouseDownHandler(evt:MouseEvent):void
99 {
100 mouseDown=true;
101 beginIndex=note.selectionBeginIndex;
102 this.note.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
103 }
104
105 public function onMoveHandler(evt:MouseEvent):void
106 {
107 if(mouseDown)
108 {
109 this.note.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
110 }
111 }
112 /* 记录选择完之后的索引值,并处理选择后的逻辑 */
113 public function onMouseUpHandler(evt:MouseEvent):void
114 {
115 endIndex=note.selectionEndIndex;
116 chooseStr=note.text.slice( beginIndex,endIndex ); //处理字符串,得到选中的词
117 result.appendText(chooseStr+"\n");
118 var temp:String="";
119 resultStr =resultStr.replace(chooseStr,temp); //把已经选择的词从整个句子中去掉-------------------------------------》》》有问题
120 }
121
122 //选择保存路径
123 protected function button3_clickHandler(event:MouseEvent):void
124 {
125 var file:File=File.documentsDirectory;
126 file.browseForDirectory("请选择保存路径");
127 file.addEventListener(Event.SELECT,onSelectDirectoryHandler);
128 }
129
130 public function onSelectDirectoryHandler(evt:Event):void
131 {
132 file2=new File(evt.target.nativePath).resolvePath("words.xml"); //在相应目录下新建一个words.txt
133 filestream.open(file2,FileMode.WRITE);
134 next.enabled=true;
135 note.selectable=true;
136 savePath.enabled=false;
137 }
138
139 ]]>
140 </fx:Script>
141
142 <s:VGroup gap="10" paddingLeft="20" paddingTop="20">
143 <s:HGroup gap="20">
144 <s:Button x="29" y="45" label="选择文件" fontSize="14" fontWeight="bold" click="button1_clickHandler(event)" id="choose" height="30" width="100" skinClass="RoundCornerButtonSkin"/>
145 <s:Button label="选择保存路径" id="savePath" enabled="false" fontSize="14" fontWeight="bold" click="button3_clickHandler(event)" height="30" width="150" skinClass="RoundCornerButtonSkin"/>
146 <s:Label text="注意:选择词语的时候必须从左到右选择,不能双击选择或者从右往左选择" fontSize="16" fontWeight="normal" color="0xff0000"/>
147 </s:HGroup>
148
149 <mx:TextArea width="800" height="50" id="note" fontSize="14" fontWeight="bold" editable="false" selectable="false"/>
150 <s:Button label="Next" toolTip="每次点击Next就将下面的文本更新到文档" fontSize="14" fontWeight="bold" click="button2_clickHandler(event)" enabled="false" id="next" height="30" width="100" skinClass="RoundCornerButtonSkin"/>
151 <s:Label text="选择结果(如果操作失误,可以直接在下面进行修改)" fontSize="14" fontStyle="italic" fontWeight="bold" color="0xFF0000"/>
152 <s:TextArea width="648" height="216" id="result"/>
153 </s:VGroup>
154 </s:WindowedApplication>

button的皮肤部分:
RoundCornerButtonSkin:

 1 <?xml version="1.0" encoding="utf-8"?>
2
3 <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
4 xmlns:s="library://ns.adobe.com/flex/spark"
5 xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
6 >
7
8 <fx:Metadata>
9 <![CDATA[
10 /**
11 * @copy spark.skins.spark.ApplicationSkin#hostComponent
12 */
13 [HostComponent("spark.components.Button")]
14 ]]>
15 </fx:Metadata>
16
17 <!-- states -->
18 <s:states>
19 <s:State name="up" />
20 <s:State name="over" />
21 <s:State name="down" />
22 <s:State name="disabled" />
23 </s:states>
24
25
26 <s:Rect id="backgroundA" radiusX="{this.height/2}" radiusY="{this.height/2}" left="0" right="0" top="0" bottom="0"
27 width="100%" height="100%"
28 >
29 <s:filters>
30 <s:GlowFilter blurX="3" blurY="3" strength="1" color="0x777777" quality="5"/>
31 </s:filters>
32 <s:fill>
33 <s:SolidColor color="0xffffff" />
34 </s:fill>
35 <s:stroke>
36 <s:SolidColorStroke color="0xe5e5e5" weight="1" />
37 </s:stroke>
38 </s:Rect>
39
40 <s:Rect id="backgourndB" width="{backgroundA.width - 5}" height="{backgroundA.height-5}" radiusX="{(backgroundA.height-5)/2}" radiusY="{(backgroundA.height-5)/2}"
41 verticalCenter="0" horizontalCenter="0"
42 >
43 <s:fill>
44 <s:LinearGradient rotation="90">
45 <s:GradientEntry color="0xff5a00" color.over="0x005ddc" color.disabled="0xa6a6a6" />
46 <s:GradientEntry color="0xbe4300" color.over="0x014bb0" color.disabled="0x7e7e7e" />
47 </s:LinearGradient>
48 </s:fill>
49 </s:Rect>
50 <s:Label id="labelDisplay"
51 textAlign="center"
52 maxDisplayedLines="1"
53 horizontalCenter="0" verticalCenter="0" verticalAlign="middle"
54 >
55 </s:Label>
56
57 </s:Skin>

 

posted @ 2012-03-24 20:55  简道云  阅读(338)  评论(0编辑  收藏  举报