Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
原文: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
- <span style="font-family:'Courier New';">package com.liu.activity;
- import android.app.Activity;
- import android.app.backup.SharedPreferencesBackupHelper;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.Toast;
- public class LoginActivity extends Activity {
- private EditText userName, password;
- private CheckBox rem_pw, auto_login;
- private Button btn_login;
- private ImageButton btnQuit;
- private String userNameValue,passwordValue;
- private SharedPreferences sp;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //去除标题
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.login);
- //获得实例对象
- sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
- userName = (EditText) findViewById(R.id.et_zh);
- password = (EditText) findViewById(R.id.et_mima);
- rem_pw = (CheckBox) findViewById(R.id.cb_mima);
- auto_login = (CheckBox) findViewById(R.id.cb_auto);
- btn_login = (Button) findViewById(R.id.btn_login);
- btnQuit = (ImageButton)findViewById(R.id.img_btn);
- //判断记住密码多选框的状态
- if(sp.getBoolean("ISCHECK", false))
- {
- //设置默认是记录密码状态
- rem_pw.setChecked(true);
- userName.setText(sp.getString("USER_NAME", ""));
- password.setText(sp.getString("PASSWORD", ""));
- //判断自动登陆多选框状态
- if(sp.getBoolean("AUTO_ISCHECK", false))
- {
- //设置默认是自动登录状态
- auto_login.setChecked(true);
- //跳转界面
- Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
- LoginActivity.this.startActivity(intent);
- }
- }
- // 登录监听事件 现在默认为用户名为:liu 密码:123
- btn_login.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- userNameValue = userName.getText().toString();
- passwordValue = password.getText().toString();
- if(userNameValue.equals("liu")&&passwordValue.equals("123"))
- {
- Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
- //登录成功和记住密码框为选中状态才保存用户信息
- if(rem_pw.isChecked())
- {
- //记住用户名、密码、
- Editor editor = sp.edit();
- editor.putString("USER_NAME", userNameValue);
- editor.putString("PASSWORD",passwordValue);
- editor.commit();
- }
- //跳转界面
- Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
- LoginActivity.this.startActivity(intent);
- //finish();
- }else{
- Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
- }
- }
- });
- //监听记住密码多选框按钮事件
- rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (rem_pw.isChecked()) {
- System.out.println("记住密码已选中");
- sp.edit().putBoolean("ISCHECK", true).commit();
- }else {
- System.out.println("记住密码没有选中");
- sp.edit().putBoolean("ISCHECK", false).commit();
- }
- }
- });
- //监听自动登录多选框事件
- auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- if (auto_login.isChecked()) {
- System.out.println("自动登录已选中");
- sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
- } else {
- System.out.println("自动登录没有选中");
- sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
- }
- }
- });
- btnQuit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- }</span>
LogoActivity.java
- <span style="font-family:'Courier New';">package com.liu.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.opengl.ETC1;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.Animation.AnimationListener;
- import android.widget.Button;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- public class LogoActivity extends Activity {
- private ProgressBar progressBar;
- private Button backButton;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 去除标题
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.logo);
- progressBar = (ProgressBar) findViewById(R.id.pgBar);
- backButton = (Button) findViewById(R.id.btn_back);
- Intent intent = new Intent(this, WelcomeAvtivity.class);
- LogoActivity.this.startActivity(intent);
- backButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- }
- </span>
WelcomeActivity.java
- <span style="font-family:'Courier New';"><span style="BACKGROUND-COLOR: rgb(240,240,240); WHITE-SPACE: pre">package com.liu.activity;</span>
- import android.app.Activity;
- import android.os.Bundle;
- public class WelcomeAvtivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcome);
- }
- }</span>
布局文件:
login.xml文件
- <span style="font-family:'Courier New';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/logo_bg"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <ImageButton
- android:id="@+id/img_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:background="@drawable/quit"/>
- <TextView
- android:id="@+id/tv_zh"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_marginLeft="12dip"
- android:layout_marginTop="10dip"
- android:gravity="bottom"
- android:text="帐号:"
- android:textColor="#000000"
- android:textSize="18sp" />
- <EditText
- android:id="@+id/et_zh"
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- android:layout_below="@id/tv_zh"
- android:layout_marginLeft="12dip"
- android:layout_marginRight="10dip" />
- <TextView
- android:id="@+id/tv_mima"
- android:layout_width="wrap_content"
- android:layout_height="35dip"
- android:layout_below="@id/et_zh"
- android:layout_marginLeft="12dip"
- android:layout_marginTop="10dip"
- android:gravity="bottom"
- android:text="密码:"
- android:textColor="#000000"
- android:textSize="18sp" />
- <EditText
- android:id="@+id/et_mima"
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- android:layout_below="@id/tv_mima"
- android:layout_marginLeft="12dip"
- android:layout_marginRight="10dip"
- android:maxLines="200"
- android:password="true"
- android:scrollHorizontally="true" />
- <CheckBox
- android:id="@+id/cb_mima"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/et_mima"
- android:layout_marginLeft="12dip"
- android:text="记住密码"
- android:textColor="#000000" />
- <CheckBox
- android:id="@+id/cb_auto"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/cb_mima"
- android:layout_marginLeft="12dip"
- android:text="自动登录"
- android:textColor="#000000" />
- <Button
- android:id="@+id/btn_login"
- android:layout_width="80dip"
- android:layout_height="40dip"
- android:layout_below="@id/et_mima"
- android:layout_alignParentRight="true"
- android:layout_alignTop="@id/cb_auto"
- android:layout_marginRight="10dip"
- android:gravity="center"
- android:text="登录"
- android:textColor="#000000"
- android:textSize="18sp"/>
- </RelativeLayout>
- </LinearLayout></span>
logo.xml文件
- <span style="font-family:'Courier New';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/logo_bg"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="3">
- <ProgressBar
- android:id="@+id/pgBar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" />
- <TextView
- android:id="@+id/tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/pgBar"
- android:layout_centerHorizontal="true"
- android:text="正在登录..."
- android:textColor="#000000"
- android:textSize="18sp" />
- </RelativeLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn_back"
- android:layout_width="70dip"
- android:layout_height="35dip"
- android:text="取消"
- android:textColor="#000000"
- android:textSize="12sp" />
- </LinearLayout>
- </LinearLayout></span>
welcome.xml
- <span style="font-family:'Courier New';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:background="@drawable/login_bg"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="登陆成功,进入用户界面"
- android:textColor="#000000"
- android:textSize="20sp" />
- </LinearLayout></span>
Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)的更多相关文章
- 基于localStorge开发登录模块的记住密码与自动登录
前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...
- php中实现记住密码下次自动登录的例子
这篇文章主要介绍了php中实现记住密码下次自动登录的例子,本文使用cookie实现记住密码和自动登录功能,需要的朋友可以参考下 做网站的时候经常会碰到要实现记住密码,下次自动登录,一周内免登陆,一个月 ...
- WinForm应用程序的开机自启、记住密码,自动登录的实现
一.思路: 1.开机自启,自然是需要用到注册表,我们需要把程序添加到电脑的注册表中去 2.记住密码,自动登录,开机自启,在页面的呈现我们都使用复选框按钮来呈现 3.数据持久化,不能是数据库,可以是sq ...
- 一个简单WPF登陆界面,包含记住密码,自动登录等功能,简洁美观
简介:这是一个自己以前用WPF设计的登陆界面,属于一个实验性的界面窗体,如果用于产品还很有不足.但也是有一点学习价值.后台代码略有复杂,但基本上都有注释 分类,略有代码经验的一般都能看懂. 登陆界面外 ...
- 数据加密实战之记住密码、自动登录和加密保存数据运用DES和MD5混合使用
MD5的简介:MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有 ...
- android记住密码和自动登陆
import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences ...
- cookie、session及实现记住密码,自动登录
在登录帐号.密码框下,有三种帐号登录模式可供选择,用户可根据自己的具体情况选择其中一种适合自己的模式. 1.网吧模式:勾选网吧模式后,登录的帐号会在歪歪注销/退出的时候自动清除,不会留在登录框中,可以 ...
- Cookie实现记住密码、自动登录
前端代码 <form id="form" action="xxx" method="post"> <div> < ...
- Android之记住密码与自动登陆实现
本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...
随机推荐
- java.util.Properties类,保存时保留注释及格式不变
原文地址:http://blog.csdn.net/benbenxiongyuan/article/details/53006097 参考地址:http://www.iteye.com/topic/1 ...
- libcurl库的简介(一)
一.Libcurl库简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP ...
- Laravel 解决在ajax 请求下不能保存session的问题
Laravel 解决在ajax 请求下不能保存session的问题 \Session::put('isLogin',true); // 你要保存的session key \Session::put(' ...
- gRPC Learning Notes
简介 更多内容参考:https://www.grpc.io/docs/guides/ gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 G ...
- PS绘制Logo
1. 2. 3. 4. 5. 6. 第2步点击“圆1”应点击图层左边的缩览图才能获取选区 7. 8. 9. 10. 11. 12.
- ARM相关概念(学习目标、分类、商业模式及半导体公司、嵌入式处理器)
1.学习ARM的目标 (1)对比X86,8051汇编,从更底层的角度去理解相关知识 (2)为后续嵌入式课程做准备 (3)了解ARM的体系结构,能够看懂ARM汇编 2.ARM分类 (1)按照版本号分类: ...
- lucky的时光助理-2017.02
好久没有更新了, 即便没有听众, 有些故事还是要说给另一个自己听! lucky小姐在这个月开始重新找工作了, 她想找一份自己喜欢的工作, 然后安安稳稳的沉寂下来,她说:她要学些东西,才不会让自己看上去 ...
- chrome 2行换行省略号 ... text-ellipse
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; 谷歌内部项目 ...
- Linux : file命令
file xxx file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程 命令选项: -b:列出辨识结果时,不显示文件名称: -c:详细显示指令执行过 ...
- ETCD成员维护
# For each machine TOKEN=my-etcd-token-1 CLUSTER_STATE=new NAME_1=etcd-node-1 NAME_2=etcd-node-2 NAM ...