原文:http://blog.csdn.net/liuyiming_/article/details/7704923

SharedPreferences介绍:

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

SharedPreferences的用法:

由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,用法见下面代码:

Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例

name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。

mode:是指定读写方式,其值有三种,分别为:

Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写

Context.MODE_WORLD_READABLE指定该SharedPreferences数据能被其他应用程序读,但不能写

Context.MODE_WORLD_WRITEABLE指定该SharedPreferences数据能被其他应用程序读写。

结果截图:


 


 


工程目录:

Java代码

LoginActivity.java

  1. <span style="font-family:'Courier New';">package com.liu.activity;
  2. import android.app.Activity;
  3. import android.app.backup.SharedPreferencesBackupHelper;
  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.text.Spannable;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.Window;
  13. import android.widget.Button;
  14. import android.widget.CheckBox;
  15. import android.widget.CompoundButton;
  16. import android.widget.CompoundButton.OnCheckedChangeListener;
  17. import android.widget.EditText;
  18. import android.widget.ImageButton;
  19. import android.widget.Toast;
  20. public class LoginActivity extends Activity {
  21. private EditText userName, password;
  22. private CheckBox rem_pw, auto_login;
  23. private Button btn_login;
  24. private ImageButton btnQuit;
  25. private String userNameValue,passwordValue;
  26. private SharedPreferences sp;
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. //去除标题
  30. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  31. setContentView(R.layout.login);
  32. //获得实例对象
  33. sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
  34. userName = (EditText) findViewById(R.id.et_zh);
  35. password = (EditText) findViewById(R.id.et_mima);
  36. rem_pw = (CheckBox) findViewById(R.id.cb_mima);
  37. auto_login = (CheckBox) findViewById(R.id.cb_auto);
  38. btn_login = (Button) findViewById(R.id.btn_login);
  39. btnQuit = (ImageButton)findViewById(R.id.img_btn);
  40. //判断记住密码多选框的状态
  41. if(sp.getBoolean("ISCHECK", false))
  42. {
  43. //设置默认是记录密码状态
  44. rem_pw.setChecked(true);
  45. userName.setText(sp.getString("USER_NAME", ""));
  46. password.setText(sp.getString("PASSWORD", ""));
  47. //判断自动登陆多选框状态
  48. if(sp.getBoolean("AUTO_ISCHECK", false))
  49. {
  50. //设置默认是自动登录状态
  51. auto_login.setChecked(true);
  52. //跳转界面
  53. Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
  54. LoginActivity.this.startActivity(intent);
  55. }
  56. }
  57. // 登录监听事件  现在默认为用户名为:liu 密码:123
  58. btn_login.setOnClickListener(new OnClickListener() {
  59. public void onClick(View v) {
  60. userNameValue = userName.getText().toString();
  61. passwordValue = password.getText().toString();
  62. if(userNameValue.equals("liu")&&passwordValue.equals("123"))
  63. {
  64. Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
  65. //登录成功和记住密码框为选中状态才保存用户信息
  66. if(rem_pw.isChecked())
  67. {
  68. //记住用户名、密码、
  69. Editor editor = sp.edit();
  70. editor.putString("USER_NAME", userNameValue);
  71. editor.putString("PASSWORD",passwordValue);
  72. editor.commit();
  73. }
  74. //跳转界面
  75. Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
  76. LoginActivity.this.startActivity(intent);
  77. //finish();
  78. }else{
  79. Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
  80. }
  81. }
  82. });
  83. //监听记住密码多选框按钮事件
  84. rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  85. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  86. if (rem_pw.isChecked()) {
  87. System.out.println("记住密码已选中");
  88. sp.edit().putBoolean("ISCHECK", true).commit();
  89. }else {
  90. System.out.println("记住密码没有选中");
  91. sp.edit().putBoolean("ISCHECK", false).commit();
  92. }
  93. }
  94. });
  95. //监听自动登录多选框事件
  96. auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  97. public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  98. if (auto_login.isChecked()) {
  99. System.out.println("自动登录已选中");
  100. sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
  101. } else {
  102. System.out.println("自动登录没有选中");
  103. sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
  104. }
  105. }
  106. });
  107. btnQuit.setOnClickListener(new OnClickListener() {
  108. @Override
  109. public void onClick(View v) {
  110. finish();
  111. }
  112. });
  113. }
  114. }</span>

LogoActivity.java

  1. <span style="font-family:'Courier New';">package com.liu.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.content.SharedPreferences.Editor;
  6. import android.opengl.ETC1;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.view.Window;
  11. import android.view.animation.AlphaAnimation;
  12. import android.view.animation.Animation;
  13. import android.view.animation.Animation.AnimationListener;
  14. import android.widget.Button;
  15. import android.widget.ImageButton;
  16. import android.widget.ImageView;
  17. import android.widget.ProgressBar;
  18. public class LogoActivity extends Activity {
  19. private ProgressBar progressBar;
  20. private Button backButton;
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. // 去除标题
  24. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  25. setContentView(R.layout.logo);
  26. progressBar = (ProgressBar) findViewById(R.id.pgBar);
  27. backButton = (Button) findViewById(R.id.btn_back);
  28. Intent intent = new Intent(this, WelcomeAvtivity.class);
  29. LogoActivity.this.startActivity(intent);
  30. backButton.setOnClickListener(new OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. finish();
  34. }
  35. });
  36. }
  37. }
  38. </span>

WelcomeActivity.java

  1. <span style="font-family:'Courier New';"><span style="BACKGROUND-COLOR: rgb(240,240,240); WHITE-SPACE: pre">package com.liu.activity;</span>
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class WelcomeAvtivity extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. // TODO Auto-generated method stub
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.welcome);
  10. }
  11. }</span>

布局文件:

login.xml文件

  1. <span style="font-family:'Courier New';"><?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. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content" >
  10. <ImageButton
  11. android:id="@+id/img_btn"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentRight="true"
  15. android:background="@drawable/quit"/>
  16. <TextView
  17. android:id="@+id/tv_zh"
  18. android:layout_width="wrap_content"
  19. android:layout_height="35dip"
  20. android:layout_marginLeft="12dip"
  21. android:layout_marginTop="10dip"
  22. android:gravity="bottom"
  23. android:text="帐号:"
  24. android:textColor="#000000"
  25. android:textSize="18sp" />
  26. <EditText
  27. android:id="@+id/et_zh"
  28. android:layout_width="fill_parent"
  29. android:layout_height="40dip"
  30. android:layout_below="@id/tv_zh"
  31. android:layout_marginLeft="12dip"
  32. android:layout_marginRight="10dip" />
  33. <TextView
  34. android:id="@+id/tv_mima"
  35. android:layout_width="wrap_content"
  36. android:layout_height="35dip"
  37. android:layout_below="@id/et_zh"
  38. android:layout_marginLeft="12dip"
  39. android:layout_marginTop="10dip"
  40. android:gravity="bottom"
  41. android:text="密码:"
  42. android:textColor="#000000"
  43. android:textSize="18sp" />
  44. <EditText
  45. android:id="@+id/et_mima"
  46. android:layout_width="fill_parent"
  47. android:layout_height="40dip"
  48. android:layout_below="@id/tv_mima"
  49. android:layout_marginLeft="12dip"
  50. android:layout_marginRight="10dip"
  51. android:maxLines="200"
  52. android:password="true"
  53. android:scrollHorizontally="true" />
  54. <CheckBox
  55. android:id="@+id/cb_mima"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_below="@id/et_mima"
  59. android:layout_marginLeft="12dip"
  60. android:text="记住密码"
  61. android:textColor="#000000" />
  62. <CheckBox
  63. android:id="@+id/cb_auto"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_below="@id/cb_mima"
  67. android:layout_marginLeft="12dip"
  68. android:text="自动登录"
  69. android:textColor="#000000" />
  70. <Button
  71. android:id="@+id/btn_login"
  72. android:layout_width="80dip"
  73. android:layout_height="40dip"
  74. android:layout_below="@id/et_mima"
  75. android:layout_alignParentRight="true"
  76. android:layout_alignTop="@id/cb_auto"
  77. android:layout_marginRight="10dip"
  78. android:gravity="center"
  79. android:text="登录"
  80. android:textColor="#000000"
  81. android:textSize="18sp"/>
  82. </RelativeLayout>
  83. </LinearLayout></span>

logo.xml文件

  1. <span style="font-family:'Courier New';"><?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. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="3">
  11. <ProgressBar
  12. android:id="@+id/pgBar"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true" />
  16. <TextView
  17. android:id="@+id/tv1"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/pgBar"
  21. android:layout_centerHorizontal="true"
  22. android:text="正在登录..."
  23. android:textColor="#000000"
  24. android:textSize="18sp" />
  25. </RelativeLayout>
  26. <LinearLayout
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:gravity="center"
  31. android:orientation="vertical" >
  32. <Button
  33. android:id="@+id/btn_back"
  34. android:layout_width="70dip"
  35. android:layout_height="35dip"
  36. android:text="取消"
  37. android:textColor="#000000"
  38. android:textSize="12sp" />
  39. </LinearLayout>
  40. </LinearLayout></span>

welcome.xml

    1. <span style="font-family:'Courier New';"><?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:layout_gravity="center"
    6. android:background="@drawable/login_bg"
    7. android:orientation="vertical" >
    8. <TextView
    9. android:layout_width="fill_parent"
    10. android:layout_height="wrap_content"
    11. android:gravity="center"
    12. android:text="登陆成功,进入用户界面"
    13. android:textColor="#000000"
    14. android:textSize="20sp" />
    15. </LinearLayout></span>

Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)的更多相关文章

  1. 基于localStorge开发登录模块的记住密码与自动登录

    前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...

  2. php中实现记住密码下次自动登录的例子

    这篇文章主要介绍了php中实现记住密码下次自动登录的例子,本文使用cookie实现记住密码和自动登录功能,需要的朋友可以参考下 做网站的时候经常会碰到要实现记住密码,下次自动登录,一周内免登陆,一个月 ...

  3. WinForm应用程序的开机自启、记住密码,自动登录的实现

    一.思路: 1.开机自启,自然是需要用到注册表,我们需要把程序添加到电脑的注册表中去 2.记住密码,自动登录,开机自启,在页面的呈现我们都使用复选框按钮来呈现 3.数据持久化,不能是数据库,可以是sq ...

  4. 一个简单WPF登陆界面,包含记住密码,自动登录等功能,简洁美观

    简介:这是一个自己以前用WPF设计的登陆界面,属于一个实验性的界面窗体,如果用于产品还很有不足.但也是有一点学习价值.后台代码略有复杂,但基本上都有注释 分类,略有代码经验的一般都能看懂. 登陆界面外 ...

  5. 数据加密实战之记住密码、自动登录和加密保存数据运用DES和MD5混合使用

    MD5的简介:MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有 ...

  6. android记住密码和自动登陆

    import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences ...

  7. cookie、session及实现记住密码,自动登录

    在登录帐号.密码框下,有三种帐号登录模式可供选择,用户可根据自己的具体情况选择其中一种适合自己的模式. 1.网吧模式:勾选网吧模式后,登录的帐号会在歪歪注销/退出的时候自动清除,不会留在登录框中,可以 ...

  8. Cookie实现记住密码、自动登录

    前端代码 <form id="form" action="xxx" method="post"> <div> < ...

  9. Android之记住密码与自动登陆实现

    本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...

随机推荐

  1. 「JSOI2015」圈地

    「JSOI2015」圈地 传送门 显然是最小割. 首先对于所有房子,权值 \(> 0\) 的连边 \(s \to i\) ,权值 \(< 0\) 的连边 \(i \to t\) ,然后对于 ...

  2. hbase60010端口无法访问web页面

    原因:HBASE1.0之后的版本web端访问的接口变更为16010

  3. mysql 存入数据库 中文乱码

    1.要保证数据库.表.字段都是utf-8的数据类型.排序一直即可. 数据库的在数据库属性里面改: 表的在设计表里面改: 字段的也是在设计表里面改: 常用命令: -- 检查字符集类型show varia ...

  4. CSS制作二级菜单时,二级菜单与一级菜单不对齐

    效果如图: 部分代码如图: <li><a href="#" target="_blank">关于我们</a> <ul& ...

  5. 动态规划: 最大m子段和问题的详细解题思路(JAVA实现)

    这道最大m子段问题我是在课本<计算机算法分析与设计>上看到,课本也给出了相应的算法,也有解这题的算法的逻辑.但是,看完之后,我知道这样做可以解出正确答案,但是我如何能想到要这样做呢? 课本 ...

  6. Python引用某一文件的方法出现红色波浪线

    from parse import parse_url#引用parse里面的方法 结果出现波浪线并提示 This inspection detects names that should resolv ...

  7. 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案

    前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...

  8. 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数

    import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) p ...

  9. Docker for YApi--一键部署YApi

    获取YApi镜像$ docker pull mrjin/yapi:latest 注意:本仓库目前只支持安装,暂不支持升级,请知晓.如需升级请备份mongoDB内的数据. docker-compose ...

  10. Java学习资源 - 其他

    http请求HttpServletRequest详解 HttpServletRequest请求转发 高并发场景下的httpClient优化使用 HttpClien高并发请求连接池 - PoolingH ...