android 利用SharedPreferences做的简单记住密码+自动登陆
SharedPreferences
1.SharePreference这个接口很是用于保存数据用的
调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个参数是文件名称,第二个参数是操作模式。
操作模式有三种:MODE_PRIVATE(私有) ,MODE_WORLD_READABLE(可读),MODE_WORLD_WRITEABLE(可写)
SharePreference提供了获得数据的方法,如getString(String key,String defValue)等
调用SharePreferences的edit()方法返回SharePreferences.Editor内部接口,该接口提供了保存数据的方法如:putString(String key,String value)等,调用该接口的commit()方法可以将
数据保存。
2.下面直接用一个例子来说明SharePreferences
MainActivity的xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/bg_login_activity" 6 android:orientation="vertical" 7 tools:context=".MainActivity" > 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_marginLeft="35dip" 13 android:layout_marginTop="150dip" 14 android:orientation="horizontal" > 15 16 <TextView 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="用户名:" 20 android:textSize="20dp" /> 21 22 <EditText 23 android:id="@+id/username" 24 android:layout_width="200dp" 25 android:layout_height="35dp" 26 android:background="@drawable/bg_input_center" /> 27 </LinearLayout> 28 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_marginLeft="35dip" 33 android:layout_marginTop="8dp" 34 android:orientation="horizontal" > 35 36 <TextView 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text=" 密码:" 40 android:textSize="20dp" /> 41 42 <EditText 43 android:id="@+id/password" 44 android:layout_width="200dp" 45 android:layout_height="35dp" 46 android:background="@drawable/bg_input_center" 47 android:password="true" /> 48 </LinearLayout> 49 50 <LinearLayout 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content" 53 android:layout_marginLeft="75dip" 54 android:layout_marginTop="8dp" 55 android:orientation="horizontal" > 56 57 <TextView 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:text="保存密码:" /> 61 62 <CheckBox 63 android:id="@+id/savePassword" 64 android:layout_width="wrap_content" 65 android:layout_height="wrap_content" /> 66 67 <TextView 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:layout_marginLeft="30dp" 71 android:text="自动登陆:" /> 72 73 <CheckBox 74 android:id="@+id/autoLogin" 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" /> 77 </LinearLayout> 78 79 <LinearLayout 80 android:layout_width="match_parent" 81 android:layout_height="wrap_content" 82 android:orientation="horizontal" 83 > 84 85 <Button 86 android:id="@+id/login_btn" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:layout_marginLeft="75dip" 90 android:text="登陆" /> 91 92 <Button 93 android:id="@+id/unlogin_btn" 94 android:layout_width="wrap_content" 95 android:layout_height="wrap_content" 96 android:layout_marginLeft="20dip" 97 android:text="退出" /> 98 </LinearLayout> 99 100 </LinearLayout>
UserInfo类:
1 package com.example.login.util; 2 3 import android.content.Context; 4 import android.content.SharedPreferences; 5 6 public class UserInfo { 7 8 private String USER_INFO = "userInfo"; 9 10 private Context context; 11 12 public UserInfo() { 13 } 14 15 public UserInfo(Context context) { 16 17 this.context = context; 18 } 19 // 存放字符串型的值 20 public void setUserInfo(String key, String value) { 21 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 22 Context.MODE_PRIVATE); 23 SharedPreferences.Editor editor = sp.edit(); 24 editor.remove(key); 25 editor.putString(key, value); 26 editor.commit(); 27 } 28 29 // 存放整形的值 30 public void setUserInfo(String key, int value) { 31 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 32 Context.MODE_PRIVATE); 33 SharedPreferences.Editor editor = sp.edit(); 34 editor.remove(key); 35 editor.putInt(key, value); 36 editor.commit(); 37 } 38 39 // 存放长整形值 40 public void setUserInfo(String key, Long value) { 41 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 42 Context.MODE_PRIVATE); 43 SharedPreferences.Editor editor = sp.edit(); 44 editor.remove(key); 45 editor.putLong(key, value); 46 editor.commit(); 47 } 48 49 // 存放布尔型值 50 public void setUserInfo(String key, Boolean value) { 51 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 52 Context.MODE_PRIVATE); 53 SharedPreferences.Editor editor = sp.edit(); 54 editor.remove(key); 55 editor.putBoolean(key, value); 56 editor.commit(); 57 } 58 59 // 清空记录 60 public void clear() { 61 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 62 Context.MODE_PRIVATE); 63 SharedPreferences.Editor editor = sp.edit(); 64 editor.clear(); 65 editor.commit(); 66 } 67 68 // 注销用户时清空用户名和密码 69 // public void logOut() { 70 // SharedPreferences sp = context.getSharedPreferences(USER_INFO, 71 // Context.MODE_PRIVATE); 72 // SharedPreferences.Editor editor = sp.edit(); 73 // editor.remove(ACCOUNT); 74 // editor.remove(PASSWORD); 75 // editor.commit(); 76 // } 77 78 // 获得用户信息中某项字符串型的值 79 public String getStringInfo(String key) { 80 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 81 Context.MODE_PRIVATE); 82 return sp.getString(key, ""); 83 } 84 85 // 获得用户息中某项整形参数的值 86 public int getIntInfo(String key) { 87 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 88 Context.MODE_PRIVATE); 89 return sp.getInt(key, -1); 90 } 91 92 // 获得用户信息中某项长整形参数的值 93 public Long getLongInfo(String key) { 94 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 95 Context.MODE_PRIVATE); 96 return sp.getLong(key, -1); 97 } 98 99 // 获得用户信息中某项布尔型参数的值 100 public boolean getBooleanInfo(String key) { 101 SharedPreferences sp = context.getSharedPreferences(USER_INFO, 102 Context.MODE_PRIVATE); 103 return sp.getBoolean(key, false); 104 } 105 106 }
MainActivity:
1 public class MainActivity extends Activity { 2 3 private Button login, cancel; 4 5 private EditText usernameEdit, passwordEdit; 6 7 private CheckBox CK_save, CK_auto; 8 9 private UserInfo userInfo; 10 11 private static final String USER_NAME = "user_name"; 12 private static final String PASSWORD = "password"; 13 private static final String ISSAVEPASS = "savePassWord"; 14 private static final String AUTOLOGIN = "autoLogin"; 15 16 private String username; 17 private String password; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 userInfo = new UserInfo(this); 25 login = (Button) findViewById(R.id.login_btn); 26 cancel = (Button) findViewById(R.id.unlogin_btn); 27 usernameEdit = (EditText) findViewById(R.id.username); 28 passwordEdit = (EditText) findViewById(R.id.password); 29 CK_save = (CheckBox) findViewById(R.id.savePassword); 30 CK_auto = (CheckBox) findViewById(R.id.autoLogin); 31 // 判断是否记住了密码的 初始默认是要记住密码的 32 if (userInfo.getBooleanInfo(ISSAVEPASS)) { 33 CK_save.setChecked(true); 34 usernameEdit.setText(userInfo.getStringInfo(USER_NAME)); 35 passwordEdit.setText(userInfo.getStringInfo(PASSWORD)); 36 // 判断是否要自动登陆 37 if (userInfo.getBooleanInfo(AUTOLOGIN)) { 38 // 默认是要自动登陆的 39 CK_auto.setChecked(true); 40 Intent i = new Intent(); 41 i.setClass(MainActivity.this, SecondActivity.class); 42 startActivity(i); 43 } 44 45 } 46 47 login.setOnClickListener(new Button.OnClickListener() { 48 49 @Override 50 public void onClick(View v) { 51 username = usernameEdit.getText().toString(); 52 password = passwordEdit.getText().toString(); 53 if (username.equals("liang") && password.equals("123")) { 54 if (CK_save.isChecked()) { 55 userInfo.setUserInfo(USER_NAME, username); 56 userInfo.setUserInfo(PASSWORD, password); 57 userInfo.setUserInfo(ISSAVEPASS, true); 58 } 59 if (CK_auto.isChecked()) { 60 userInfo.setUserInfo(AUTOLOGIN, true); 61 62 } 63 Intent i = new Intent(); 64 i.setClass(MainActivity.this, SecondActivity.class); 65 startActivity(i); 66 } 67 68 } 69 }); 70 71 } 72 }
运行后的界面:
点击登录后从第一张界面跳跳转到第二张界面,关闭应用后在点击应用就直接显示的是第二张界面了。 当直接显示第二张界面的时候按back键后,就回到第一张界面,这就说明了是自动从第一张界面跳转到第二张界面的