ActionScript 实现用户登录时记住用户名和密码

实现效果:用户登录成功之后,程序将记录用户密码。当程序关闭,下次在启动的时候,将自动显示用户上次登录用的用户名和密码。

代码如下:

 1 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
2 xmlns:s="library://ns.adobe.com/flex/spark"
3 xmlns:mx="library://ns.adobe.com/flex/mx"
4 minWidth="955" minHeight="600" creationComplete="creationComplete()">
5 <fx:Declarations>
6 <!-- 将非可视元素(例如服务、值对象)放在此处 -->
7 </fx:Declarations>
8 <fx:Script>
9 <![CDATA[
10 import flash.system.fscommand;
11 public var localStore:SharedObject;
12 public var users:Object;
13
14 public function creationComplete():void
15 {
16 localStore=SharedObject.getLocal("MyApp","/"); //获取本地存储对象
17 userName.text=localStore.data["Name"];
18 password_txt.text=localStore.data["Password"];
19 //初始化用户列表
20 users=localStore.data["Users"];
21 if(users==null)
22 {
23 users={tom:"abc",jurry:"qwer"};
24 }
25 }
26
27 public function onLogin(evt:MouseEvent):void
28 {
29 localStore.data["Name"]=userName.text;
30 if(users[userName.text.toLowerCase()]==password_txt.text.toLowerCase())
31 {
32 localStore.data["Password"]=password_txt.text; //如果登录成功,将密码保存。
33 localStore.flush(); //为了把数据立即存储到本地硬盘
34 msg.text="用户登录成功!";
35 }
36 else
37 {
38 msg.text="登录失败,请重新填写用户名和密码。";
39 }
40 }
41
42 //退出程序
43 public function onCancle(evt:MouseEvent):void
44 {
45 localStore.close();
46 fscommand("quit","");
47 }
48
49 //清理本地缓存
50 public function onDelete(evt:MouseEvent):void
51 {
52 localStore.clear();
53 userName.text="";
54 password_txt.text="";
55 }
56
57 //打开本地存储设置
58 public function onSet(evt:MouseEvent):void
59 {
60 Security.showSettings(SecurityPanel.LOCAL_STORAGE);
61 }
62 ]]>
63 </fx:Script>
64 <s:Form x="100" y="100">
65 <s:FormItem label="用户名">
66 <s:TextInput id="userName"/>
67 </s:FormItem>
68 <s:FormItem label="密码">
69 <s:TextInput id="password_txt" displayAsPassword="true"/>
70 </s:FormItem>
71 <s:FormItem>
72 <s:HGroup>
73 <s:Button id="login" label="登录" click="onLogin(event)"/>
74 <s:Button id="cancle" label="取消" click="onCancle(event)"/>
75 <s:Button id="delete" label="清除" click="onDelete(event)"/>
76 <s:Button id="set" label="设置" click="onSet(event)"/>
77 </s:HGroup>
78 <s:Label id="msg"/>
79 </s:FormItem>
80 </s:Form>
81 </s:Application>

 

posted @ 2012-03-15 17:34  简道云  阅读(950)  评论(0编辑  收藏  举报