Android记住密码自动登录的实现

我采用的是SharedPreferences来存取数据的,所以先简单的介绍一下SharedPreferences

SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。

获取SharedPreferences的两种方式:
1 调用Context对象的getSharedPreferences()方法
2 调用Activity对象的getPreferences()方法
两种方式的区别:
调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.
 
SharedPreferences的四种操作模式:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
 
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
 
由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,
 
将数据保存至SharedPreferences:
SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);
Editor editor=preferences.edit();
String name="xixi";
String age="22";
editor.putString("name", name);
editor.putString("age", age);
editor.commit();
 
从SharedPreferences获取数据:
SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
String name=preferences.getString("name", "defaultname");
String age=preferences.getString("age", "0");

 

下面进入正题,实现记住密码并自动登录

 新建login.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent"  
 5     android:background="@drawable/logo_bg"  
 6     android:orientation="vertical" >  
 7   
 8     <RelativeLayout  
 9         android:layout_width="fill_parent"  
10         android:layout_height="wrap_content" >  
11         <ImageButton   
12             android:id="@+id/img_btn"  
13             android:layout_width="wrap_content"  
14             android:layout_height="wrap_content"  
15             android:layout_alignParentRight="true"  
16             android:background="@drawable/quit"/>  
17   
18         <TextView  
19             android:id="@+id/tv_zh"  
20             android:layout_width="wrap_content"  
21             android:layout_height="35dip"  
22             android:layout_marginLeft="12dip"  
23             android:layout_marginTop="10dip"  
24             android:gravity="bottom"  
25             android:text="帐号:"  
26             android:textColor="#000000"  
27             android:textSize="18sp" />  
28   
29         <EditText  
30             android:id="@+id/et_zh"  
31             android:layout_width="fill_parent"  
32             android:layout_height="40dip"  
33             android:layout_below="@id/tv_zh"  
34             android:layout_marginLeft="12dip"  
35             android:layout_marginRight="10dip" />  
36   
37         <TextView  
38             android:id="@+id/tv_mima"  
39             android:layout_width="wrap_content"  
40             android:layout_height="35dip"  
41             android:layout_below="@id/et_zh"  
42             android:layout_marginLeft="12dip"  
43             android:layout_marginTop="10dip"  
44             android:gravity="bottom"  
45             android:text="密码:"  
46             android:textColor="#000000"  
47             android:textSize="18sp" />  
48   
49         <EditText  
50             android:id="@+id/et_mima"  
51             android:layout_width="fill_parent"  
52             android:layout_height="40dip"  
53             android:layout_below="@id/tv_mima"  
54             android:layout_marginLeft="12dip"  
55             android:layout_marginRight="10dip"  
56             android:maxLines="200"  
57             android:password="true"  
58             android:scrollHorizontally="true" />  
59   
60         <CheckBox  
61             android:id="@+id/cb_mima"  
62             android:layout_width="wrap_content"  
63             android:layout_height="wrap_content"  
64             android:layout_below="@id/et_mima"  
65             android:layout_marginLeft="12dip"  
66             android:text="记住密码"  
67             android:textColor="#000000" />  
68   
69         <CheckBox  
70             android:id="@+id/cb_auto"  
71             android:layout_width="wrap_content"  
72             android:layout_height="wrap_content"  
73             android:layout_below="@id/cb_mima"  
74             android:layout_marginLeft="12dip"  
75             android:text="自动登录"  
76             android:textColor="#000000" />  
77         <Button  
78             android:id="@+id/btn_login"  
79             android:layout_width="80dip"  
80             android:layout_height="40dip"  
81             android:layout_below="@id/et_mima"  
82             android:layout_alignParentRight="true"  
83             android:layout_alignTop="@id/cb_auto"  
84             android:layout_marginRight="10dip"  
85             android:gravity="center"  
86             android:text="登录"  
87             android:textColor="#000000"  
88             android:textSize="18sp"/>  
89   
90           
91     </RelativeLayout>  
92       
93       
94   
95 </LinearLayout>

 新建logo.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent"  
 5     android:background="@drawable/logo_bg"  
 6     android:orientation="vertical" >  
 7   
 8     <RelativeLayout  
 9         android:layout_width="fill_parent"  
10         android:layout_height="wrap_content"   
11         android:layout_weight="3">  
12   
13         <ProgressBar  
14             android:id="@+id/pgBar"  
15             android:layout_width="wrap_content"  
16             android:layout_height="wrap_content"  
17             android:layout_centerInParent="true" />  
18   
19         <TextView  
20             android:id="@+id/tv1"  
21             android:layout_width="wrap_content"  
22             android:layout_height="wrap_content"  
23             android:layout_below="@id/pgBar"  
24             android:layout_centerHorizontal="true"  
25             android:text="正在登录..."  
26             android:textColor="#000000"  
27             android:textSize="18sp" />  
28     </RelativeLayout>  
29   
30     <LinearLayout  
31         android:layout_width="fill_parent"  
32         android:layout_height="wrap_content"  
33         android:layout_weight="1"  
34         android:gravity="center"  
35         android:orientation="vertical" >  
36   
37         <Button  
38             android:id="@+id/btn_back"  
39             android:layout_width="70dip"  
40             android:layout_height="35dip"  
41             android:text="取消"  
42             android:textColor="#000000"  
43             android:textSize="12sp" />  
44     </LinearLayout>  
45   
46   
47 </LinearLayout>

新建类LoginActivity

  1 package com.example.rixin;
  2 
  3 import android.app.Activity;
  4 import android.content.Context;
  5 import android.content.Intent;
  6 import android.content.SharedPreferences;
  7 import android.content.SharedPreferences.Editor;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 import android.view.View.OnClickListener;
 11 import android.view.Window;
 12 import android.widget.Button;
 13 import android.widget.CheckBox;
 14 import android.widget.CompoundButton;
 15 import android.widget.CompoundButton.OnCheckedChangeListener;
 16 import android.widget.EditText;
 17 import android.widget.ImageButton;
 18 import android.widget.Toast;
 19 
 20 public class LoginActivity extends Activity{
 21     private SharedPreferences sp;
 22     private EditText userName,password;
 23     private CheckBox rem_pw, auto_login;
 24     private Button btn_login;
 25     private ImageButton btnQuit;
 26     private String userNameValue,passwordValue; 
 27     
 28     public void onCreate(Bundle savedInstanceState){
 29         super.onCreate(savedInstanceState);
 30         
 31         //去除标题
 32         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 33         setContentView(R.layout.login);
 34         
 35         //获取实例化对象
 36         sp=this.getSharedPreferences("userinfo", Context.MODE_WORLD_READABLE);
 37         userName=(EditText) findViewById(R.id.et_zh);
 38         password = (EditText) findViewById(R.id.et_mima);
 39         rem_pw = (CheckBox) findViewById(R.id.cb_mima);
 40         auto_login = (CheckBox) findViewById(R.id.cb_auto);
 41         btn_login = (Button) findViewById(R.id.btn_login);
 42         btnQuit = (ImageButton)findViewById(R.id.img_btn);
 43         
 44         //判断记住多选框的状态
 45         if (sp.getBoolean("ISCHECK", false)) {//假设SharedPreferences里面还没有ISCHECK的值,此时默认返回false
 46             //设置默认是记录密码
 47             rem_pw.setChecked(true);
 48             userName.setText(sp.getString("USER_NAME",""));
 49             password.setText(sp.getString("PASSWORD", ""));
 50               //判断自动登陆多选框状态  
 51             if (sp.getBoolean("AUTO_ISCHECK", false)) {
 52                 // 设置默认是自动登录状态
 53                 auto_login.setChecked(true);
 54                 // 跳转界面
 55                 Intent intent = new Intent(LoginActivity.this,
 56                         logoActivity.class);
 57                 LoginActivity.this.startActivity(intent);
 58 
 59             }        
 60         }
 61         
 62         //登录监听事件  现在默认为用户名为:rain 密码:123
 63         btn_login.setOnClickListener(new OnClickListener() {
 64             
 65             @Override
 66             public void onClick(View arg0) {
 67                 // TODO Auto-generated method stub
 68                 userNameValue=userName.getText().toString();
 69                 passwordValue=password.getText().toString();
 70                 
 71                 if (userNameValue.equals("rain")&&passwordValue.equals("123")) {
 72                     Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
 73                     //登录成功和记住密码框为选中状态才保存用户信息
 74                     if (rem_pw.isChecked()) {
 75                         //记住用户名、密码
 76                         Editor editor=sp.edit();
 77                         editor.putString("USER_NAME", userNameValue);
 78                         editor.putString("PASSWORD", passwordValue);
 79                         editor.commit();
 80                     }
 81                     //跳转界面
 82                     Intent intent =new Intent(LoginActivity.this,logoActivity.class);
 83                     LoginActivity.this.startActivity(intent);
 84                 }else {
 85                     Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
 86                 }                
 87                 
 88             }
 89         });
 90         
 91         //监听记住密码多选框按钮事件
 92         rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
 93             
 94             @Override
 95             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
 96                 // TODO Auto-generated method stub
 97                 if (rem_pw.isChecked()) {
 98                     Toast.makeText(LoginActivity.this,"记住密码已选中", Toast.LENGTH_SHORT).show();
 99                     sp.edit().putBoolean("ISCHECK", true).commit();
100                 } else {
101                     Toast.makeText(LoginActivity.this,"记住密码没有选中", Toast.LENGTH_SHORT).show();
102                     sp.edit().putBoolean("ISCHECK", false).commit();
103                 }
104             }
105         });
106         
107         //监听自动登录多选框事件
108         auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
109             
110             @Override
111             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
112                 // TODO Auto-generated method stub
113                 if (auto_login.isChecked()) {
114                     Toast.makeText(LoginActivity.this, "自动登录已选中", Toast.LENGTH_SHORT).show();
115                     sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
116                     boolean b = sp.getBoolean("AUTO_ISCHECK", false);
117                 } else {
118                     Toast.makeText(LoginActivity.this, "自动登录未选中", Toast.LENGTH_SHORT).show();
119                     sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
120                 }
121             }
122         });
123         
124         btnQuit.setOnClickListener(new OnClickListener() {
125             
126             @Override
127             public void onClick(View arg0) {
128                 // TODO Auto-generated method stub
129                 finish();
130             }
131         });      
135     }
140 }

新建logoActivity类

 1 package com.example.rixin;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.view.Window;
 9 import android.widget.Button;
10 import android.widget.ProgressBar;
11 
12 public class logoActivity extends Activity{
13     private ProgressBar progressBar;
14     private Button backButton;
15     
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         //去除标题  
19         this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
20         setContentView(R.layout.logo);
21         
22         progressBar=(ProgressBar) findViewById(R.id.pgBar);
23         backButton=(Button) findViewById(R.id.btn_back);
24         
25         Intent intent=new Intent(this,MainActivity.class);  //MainActivity是自己的主函数
26         logoActivity.this.startActivity(intent);
27         backButton.setOnClickListener(new OnClickListener() {
28               
29             @Override  
30             public void onClick(View v) {
31                 finish(); 
32             }
34         });    
35     }
36 }

运行结果如下:(在logo界面因为运行太快,所以没有看到特别明显的过渡变化,)

 

posted @ 2016-05-03 15:40  下雨天rain  阅读(1074)  评论(0编辑  收藏  举报