免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5237 | 回复: 0
打印 上一主题 下一主题

android开发,以记住用户登录密码为例说明SharedPreferences的简单应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-21 20:34 |只看该作者 |倒序浏览
android开发,以记住用户登录密码为例说明SharedPreferences的简单应用



SharedPreferences是以键值对来存储应用程序的配置信息的一种方式,以下以记住用户登录密码为例,来说明SharedPreferences的简单应用。

代码如下:
  1. 1 package com.android.test;
  2.   2
  3.   3
  4.   4 import android.app.Activity;
  5.   7 import android.content.Intent;
  6.   8 import android.content.SharedPreferences;
  7.   9 import android.content.SharedPreferences.Editor;
  8. 10 import android.os.Bundle;
  9. 13 import android.view.View;
  10. 15 import android.widget.CheckBox;
  11. 16 import android.widget.EditText;
  12. 17
  13. 24
  14. 25 /**
  15. 26  * 登录界面
  16. 27  */
  17. 28 public class LoginActivity extends Activity {
  18. 29
  19. 30     private static final String PREFS_NAME = "MyUserInfo";
  20. 34     private CheckBox chkSaveInfo;
  21. 35     private EditText txtUserName;
  22. 36     private EditText txtPassword;
  23. 38     
  24. 73     
  25. 74     /** Called when the activity is first created. */
  26. 75     @Override
  27. 76     public void onCreate(Bundle savedInstanceState) {
  28. 77         super.onCreate(savedInstanceState);
  29. 78         setContentView(R.layout.loginview);

  30. 82         chkSaveInfo = (CheckBox) this.findViewById(R.id.chkSaveInfo);
  31. 83         txtUserName = (EditText) this.findViewById(R.id.txtUserName);
  32. 84         txtPassword = (EditText) this.findViewById(R.id.txtPassword);
  33. 87         
  34. 88         LoadUserData();
  35. 89     }
  36. 90
  37. 91     /**
  38. 92      * 载入已记住用户信息
  39. 93      */
  40. 94     private void LoadUserData(){
  41.             //载入配置文件
  42. 95         SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
  43.             //读取配置文件
  44. 96         if (sp.getBoolean("isSave", false)){
  45. 97             String userName = sp.getString("userName", "");
  46. 98             String userPassword = sp.getString("userPassword", "");
  47. 99             if (!("".equals(userName) && "".equals(userPassword))){
  48. 100                 txtUserName.setText(userName);
  49. 101                 txtPassword.setText(userPassword);
  50. 102                 chkSaveInfo.setChecked(true);
  51. 103             }
  52. 104         }
  53. 105     }  
  54. 107     /**
  55. 108      * 保存用户信息
  56. 109      */
  57. 110     private void SaveUserData(){
  58.             //载入配置文件
  59. 111         SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
  60.             //写入配置文件
  61. 112         Editor spEd = sp.edit();
  62. 113         if (chkSaveInfo.isChecked()){
  63. 114             spEd.putBoolean("isSave", true);
  64. 115             spEd.putString("userName", txtUserName.getText().toString());
  65. 116             spEd.putString("userPassword", txtPassword.getText().toString());
  66. 117         }
  67. 118         else{
  68. 119             spEd.putBoolean("isSave", false);
  69. 120             spEd.putString("userName", "");
  70. 121             spEd.putString("userPassword", "");
  71. 122         }
  72. 123         spEd.commit();
  73. 124     }
  74. 209 }

复制代码
代码是从项目里摘出来的,只用于说明问题,请见谅。欢迎指正,不胜感激。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP