用flex 4 做了一个飞信,功能很简单,只能发送

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" width="540" height="400" creationComplete="init();"
					   xmlns:net="flash.net.*" close="onClosingEvent(event)" mouseDown="this.nativeWindow.startMove()">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Style source="Fetion.css"/>

	<fx:Script>
		<![CDATA[
			import flash.filesystem.*;
			import mx.containers.ControlBar;
			import mx.containers.VBox;
			import mx.controls.Alert;
			import mx.controls.List;
			import mx.controls.Spacer;
			import mx.core.UIComponent;
			import mx.events.CloseEvent;
			import mx.managers.PopUpManager;
			
			import spark.components.TitleWindow;
			import spark.events.TitleWindowBoundsEvent;
			protected function titleWin_closeHandler(evt:CloseEvent):void {
				PopUpManager.removePopUp(evt.currentTarget as UIComponent);
			}
			
			protected function btn_clickHandler(evt:MouseEvent):void {
				PopUpManager.addPopUp(titleWin, this, true);
				PopUpManager.centerPopUp(titleWin);
			}
			
			
			private var user_xml:XML;
			private var myXMLURL:URLRequest;
			private var myLoader:URLLoader;
			private var so:SharedObject = SharedObject.getLocal("UserInfo");
			
			private var panel:Panel;
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var pattern:RegExp = /\d{11}/;
				if(pwd.text==""){
					Alert.show("密码不能为空,请输入密码","提示信息");
				}
				if(pattern.test(myNum.text)&&pattern.test(yourNum.text)){
					
					//根据飞信api拼写url
					var _myNum:String = myNum.text;
					var _pwd:String  = pwd.text;
					var _yourNum:String = yourNum.text;
					var _message:String = message.text;
					var url:String = "https://sms.api.bz/fetion.php?username="+ _myNum+"&password="+ _pwd +"&sendto="+ _yourNum +"&message=" + _message;
					//发送信息
					var _urlRequest:URLRequest = new URLRequest(url);
					var _urlLoader:URLLoader = new URLLoader();
					_urlLoader.load(_urlRequest);
					Alert.show("发送成功!","提示信息");
					message.text = "" ;
				}else{
					Alert.show("请检查输入的号码!输入的内容不是手机号","提示信息");
				}
			}
			
			public function selectAll(it:spark.components.TextInput):void{
				it.selectAll();
			}
			
			//启动时的初始化设置
			private function init():void{
				initApplication();
				//取得本地用户信息
				myNum.text = so.data.myNum;
				yourNum.text = so.data.yourNum;
				pwd.text = so.data.pwd;
				
				//给textarea 加入 enter监听
				message.addEventListener(KeyboardEvent.KEY_DOWN,sendMessageUseEnterKey);   
				if(so.data.selected){
					isSave.selected = true;
				}else{
					isSave.selected = false ;
				}
				Alert.show("欢迎您使用本软件,如果有什么问题或者建议,请联系Email:[email protected],谢谢!","提示");
				//初始化用户列表
				if(so.data.init!=1){
					nodes = <root>
						  <node label="1">
						    <name>许琦</name>
						    <tel>123</tel>
						  </node>
						</root>
					var file1:File = File.applicationStorageDirectory; 
					file1 = file1.resolvePath("user.xml");
					var fileStream1:FileStream = new FileStream(); 
					fileStream1.open(file1, FileMode.WRITE);
					fileStream1.writeUTFBytes(nodes.toXMLString()); 
					fileStream1.close();
					
					so.data.init=1;
				}else{
					var file2:File = File.applicationStorageDirectory;
					file2 = file2.resolvePath("user.xml");
					var fileStream2:FileStream = new FileStream(); 
					fileStream2.open(file2, FileMode.READ); 
					nodes = XML(fileStream2.readUTFBytes(fileStream2.bytesAvailable));
					fileStream2.close();
				}
			}
			
			private function trayClick():void{
				
			}
			//监听到按键执行事件
			private function sendMessageUseEnterKey(evt:KeyboardEvent):void
			{
				if(evt.keyCode==13)
				{
					var pattern:RegExp = /\d{11}/;
					if(pwd.text==""){
						Alert.show("密码不能为空,请输入密码","提示信息");
					}
					if(pattern.test(myNum.text)&&pattern.test(yourNum.text)){
						
						//根据飞信api拼写url
						var _myNum:String = myNum.text;
						var _pwd:String  = pwd.text;
						var _yourNum:String = yourNum.text;
						var _message:String = message.text;
						var url:String = "https://sms.api.bz/fetion.php?username="+ _myNum+"&password="+ _pwd +"&sendto="+ _yourNum +"&message=" + _message;
						//发送信息
						var _urlRequest:URLRequest = new URLRequest(url);
						var _urlLoader:URLLoader = new URLLoader();
						_urlLoader.load(_urlRequest);
						Alert.show("发送成功!","提示信息");
						message.text = "" ;
					}else{
						Alert.show("请检查输入的号码!输入的内容不是手机号","提示信息");
					}
				}
			}

			
			private function onClosingEvent(event:Event):void{
				
				//保存用户数据
				if(isSave.selected){
					so.data.myNum = myNum.text;
					so.data.yourNum = yourNum.text;
					so.data.pwd = pwd.text;
					so.data.selected = true ;
				}else{
					so.data.myNum = "";
					so.data.yourNum = "";
					so.data.pwd = "";
					so.data.selected = false ;
				}
				saveXML();
				this.close();
			}
			
			//添加用户里确认按钮的click
			protected function button2_clickHandler(event:MouseEvent):void
			{
				var newBeginNode:XML = <node label={nodes.children().length()+1}>
											<name>{username.text}</name>
											<tel>{telephone.text}</tel>
										</node>
				nodes.appendChild(newBeginNode);
				xmlListColl.source = nodes.children() ;
//				fileReference.save(nodes, "user.xml");
				saveXML();
				PopUpManager.removePopUp(titleWin);
			}
			
			private function logout(event:CloseEvent):void
			{
				if(event.detail == Alert.YES)
				{
					var deleteNode:int = common.selectedIndex;
					delete nodes.node[deleteNode];
					xmlListColl.source = nodes.children() ;
					saveXML();
				}
			}
			
			private function getItemValue(evt:Event):void{
//				Alert.show("你点了common.labelField: ["+common.labelField + ']  common.selectedIndex: ['+ common.selectedIndex + '] list1.selectedItem: ['+common.selectedItem+']');
				if(common.selectedIndex!=-1){
					Alert.show("确认删除吗?","重要提示",Alert.YES|Alert.NO,null,logout);
				}else{
					Alert.show("请选择要删除的好友!");
				}
			}
			private function saveXML():void{
				var file:File = File.applicationStorageDirectory; 
				file = file.resolvePath("user.xml");
				var fileStream:FileStream = new FileStream(); 
				fileStream.open(file, FileMode.WRITE);
				fileStream.writeUTFBytes(nodes.toXMLString()); 
				fileStream.close(); 
			} 
			
			private function readXML():void{
				var file:File = File.applicationStorageDirectory; 
				file = file.resolvePath("user.xml");
				var fileStream:FileStream = new FileStream(); 
				fileStream.open(file, FileMode.READ); 
				nodes = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
				fileStream.close();
			}
			/*
			*
			*
			*/
			private var dockImage:BitmapData;  
			//初始化Application里调用此方法,完成上面的第一步:  
			public function initApplication():void{  
				var loader:Loader=new Loader();  
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,prepareForSystray);//这里就是完成第一步的任务须,这个prepareForSystray就是对托盘的生在和菜单的控制  
				loader.load(new URLRequest("1.gif"));//这里先要加载托盘图标的小图片  
				this.addEventListener(Event.CLOSING,closingApplication);//设置关闭体的事件  
				
			}  
			//关闭窗体的事件  
			public function closingApplication(event:Event):void{  
				event.preventDefault();//阻止默认的事件  
				Alert.yesLabel="关闭";  
				Alert.noLabel="最小化";  
				Alert.show("关闭还是最小化","关闭吗?", 3, this, alertCloseHandler);//弹出自定义的选择框, 关于Alert的详细用法,参考官方文档或我前面的相关文章.  
			}  
			//根据用户的选择来判断做什么,这里选择是就是关闭,选择否(Mini)就是最小化到托盘.  
			private function alertCloseHandler(event:CloseEvent):void{
				saveXML();
				if(event.detail==Alert.YES){  
					closeApp(event);  
				}else{  
					dock();//最小化到托盘  
				}  
			}  
			//生成托盘  
			public function prepareForSystray(event:Event):void{  
				dockImage=event.target.content.bitmapData;  
				if(NativeApplication.supportsSystemTrayIcon){  
					setSystemTrayProperties();//设置托盘菜单的事件  
					SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();//生成托盘菜单  
				}     
			}  
			
			public function createSystrayRootMenu():NativeMenu{  
				var menu:NativeMenu = new NativeMenu();  
				var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("打开");//生成OPEN菜单项  
				var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("退出");//同理  
				openNativeMenuItem.addEventListener(Event.SELECT, undock);  
				exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);//添加EXIT菜单项事件  
				menu.addItem(openNativeMenuItem);  
				menu.addItem(new NativeMenuItem("",true));//separator   
				menu.addItem(exitNativeMenuItem);//将菜单项加入菜单  
				return menu;  
			}  
			
			//设置托盘图标的事件  
			private function setSystemTrayProperties():void{  
				SystemTrayIcon(NativeApplication.nativeApplication .icon).tooltip = "PP飞信";  
				SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);  
				stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized);   
			}  
			
			//最小化窗体  
			private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {  
				if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {  
					displayStateEvent.preventDefault();//阻止系统默认的关闭窗体事件  
					dock();//将程序放入托盘  
				}  
			}  
			
			//将本地应用程序放到托盘  
			public function dock():void {  
				stage.nativeWindow.visible = false; //设置本地程序窗体不可见  
				NativeApplication.nativeApplication.icon.bitmaps = [dockImage];//设置托盘的图标  
				
			}  
			//激活程序窗体  
			public function undock(evt:Event):void {  
				stage.nativeWindow.visible = true;//设置本地程序窗体可见  
				stage.nativeWindow.orderToFront();//设置本地程序窗体到最前端  
				NativeApplication.nativeApplication .icon.bitmaps = [];//将托盘图标清空  
			}  
			//关闭程序窗体  
			private function closeApp(evt:Event):void {  
				stage.nativeWindow.close();  
			}  

		]]>
	</fx:Script>

	<fx:Declarations>
		<net:FileReference id="fileReference" />
		<fx:XML id="nodes"/>
		<s:TitleWindow id="titleWin"
					   title="添加用户"
					   width="320" height="150"
					   close="titleWin_closeHandler(event);" >
			<s:layout>
				<s:HorizontalLayout verticalAlign="middle" paddingLeft="10" paddingRight="10"
									paddingTop="10" paddingBottom="10" />
			</s:layout>
			<s:Label text="名字:" fontSize="16"/>
			<mx:TextInput id="username" fontSize="16" width="60" height="25"/>
			<s:Label text="手机号:" fontSize="16"/>
			<s:TextInput id="telephone" fontSize="16" width="120" height="25"/>
			<s:controlBarLayout>
				<s:HorizontalLayout verticalAlign="contentJustify" paddingLeft="70" paddingRight="10"
									paddingTop="10" paddingBottom="10" />
			</s:controlBarLayout>
			<s:controlBarContent>
				<s:Button x="381" y="305" label="确定" width="60"  fontSize="16" left="30" top="10" bottom="10" click="button2_clickHandler(event)"/>
				<s:Button x="454" y="305" label="取消" width="60" fontSize="16" right="30" top="10" bottom="10" click="{PopUpManager.removePopUp(titleWin)}"/>
			</s:controlBarContent>
		</s:TitleWindow>
	</fx:Declarations> 
	<s:Panel id="main" left="1" right="1" top="0" bottom="1" title="PP飞信" fontSize="16">
		<s:Group x="10" y="37" width="343" height="81" color="#0A1416" rollOverColor="#5C99F4">
			<s:Label x="13" y="15" text="我的号码:" fontSize="16"/>
			<s:TextInput id="myNum" x="82" y="10" fontWeight="bold" textAlign="right" width="120" height="25" focusIn="selectAll(myNum);" fontSize="18"/>
			<s:Label x="214" y="15" text="密码:" fontSize="16"/>
			<s:TextInput id="pwd" x="252" y="10" height="25" width="81" displayAsPassword="true" focusIn="selectAll(pwd)" fontSize="18" textAlign="right"/>
			<s:Label x="12" y="50" text="对方号码:" fontSize="16"/>
			<s:TextInput id="yourNum" x="82" y="46" width="120" height="25" textAlign="right" fontWeight="bold" text="{common.selectedItem.tel}" focusIn="selectAll(yourNum)" fontSize="18"/>
			<s:CheckBox id="isSave" x="230" y="45" label="保存密码" fontSize="16"/>

		</s:Group>
		<s:Label x="25" y="12" text="用户信息" fontSize="16"/>
		<s:Label x="25" y="126" text="发送消息:" fontSize="16"/>
		<mx:TextArea id="message" x="10" y="149" width="343" height="149" text="{alwaysSay.selectedItem.value}" fontSize="16" />
		<s:Button x="216" y="309" label="发送" click="button1_clickHandler(event)" fontSize="16" width="60"/>
		<s:Label x="11" y="312" text="常用短语:" fontSize="16"/>
		<mx:VRule x="363" y="20" height="301"/>
		
		<mx:Accordion id="accorion" color="0x323232" width="153" height="288" y="10" x="373" fontSize="16">
			<!-- Define each panel using a VBox container. -->
			<mx:VBox label="常用联系人" fontSize="16">
				<s:List id="common" width="151" height="263" textAlign="left" labelField="name" fontSize="16">
					<s:XMLListCollection id="xmlListColl" source="{nodes.children()}" />
				</s:List>
			</mx:VBox>
		</mx:Accordion>
		<s:DropDownList id="alwaysSay" x="82" y="307" height="25" width="120" labelField="name" prompt="请选择" fontSize="16">
			<s:dataProvider>
				<mx:ArrayCollection>
					<fx:Object name="我想你了" value="我想你了"/>
					<fx:Object name="你好!" value="你好!"/>
					<fx:Object name="在干吗" value="在干吗"/>
					<fx:Object name="上网说" value="上网说"/>
					<fx:Object name="我爱你" value="我爱你"/>
				</mx:ArrayCollection>
			</s:dataProvider>
		</s:DropDownList>
		<s:Button x="381" y="305" label="添加" width="60" click="btn_clickHandler(event);" fontSize="16"/>
		<s:Button x="454" y="305" label="删除" width="60" click="getItemValue(event);" fontSize="16"/>
		<s:Button x="288" y="309" label="退出" width="60" click="closingApplication(event);"/>
	</s:Panel>
</s:WindowedApplication>

 

你可能感兴趣的:(用flex 4 做了一个飞信,功能很简单,只能发送)