Android——SharedPreferences实现登录界面的记住密码和自动登录功能

一、项目开发

1、项目运行效果图


2、开发概要

       主要功能SharedPreferences介绍,其是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。

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:表示当前文件可以被其他应用写入
       本项目中利用的是Context.MODE_PRIVATE模式,项目运行初始化是默认的记住密码状态,当勾选自动登录的时候记住密码会自动勾选,当以记住密码的状态正确登录过一次之后,系统会记住密码,将数据信息存入Android系统,ShredPreferences中的信息以XML文件的形式保存在  /data/data/cn.edu.bzu.aminiTwitter/userInfo目录下。当系统以自动登录状态正确登录过一次之后,那么系统下一次登录就会自动调取数据进入ShowActivity.xml中。

二、开发Android-SharedPreferences程序讲解

1、项目文件如下:


2、项目开发步骤

   1、新建一个项目名字为aminiTitter,在布局文件activity_main.xml中设置文件布局,主要代码如下:

        

[java]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/background_login"  
  7.     android:orientation="vertical"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  10.     android:paddingRight="@dimen/activity_horizontal_margin"  
  11.     android:paddingTop="@dimen/activity_vertical_margin"  
  12.     tools:context=".MainActivity" >  
  13.   
  14.     <RelativeLayout  
  15.         android:id="@+id/login_div"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_margin="15dip"  
  19.         android:background="@drawable/background_login_div_bg"  
  20.         android:padding="2dp">  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tvUserName"  
  24.             style="@style/normalText"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_alignParentLeft="true"  
  28.             android:layout_alignParentTop="true"  
  29.             android:layout_marginTop="5dp"  
  30.             android:text="@string/tvUserName" />  
  31.         //用户名  
  32.         <EditText  
  33.             android:id="@+id/etName"  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_below="@+id/tvUserName"  
  37.             android:hint="@string/tvUser_hint"  
  38.             android:background="@drawable/background_login_div_bg"  
  39.             android:inputType="text"  
  40.             android:singleLine="true" >  
  41.         </EditText>  
  42.   
  43.         <TextView  
  44.             android:id="@+id/tvPasswordName"  
  45.             style="@style/normalText"  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content"  
  48.             android:layout_alignLeft="@+id/etName"  
  49.             android:layout_below="@+id/etName"  
  50.             android:layout_marginTop="5dp"  
  51.             android:text="@string/tvPasswordName" />  
  52.          //密码  
  53.         <EditText  
  54.             android:id="@+id/etPassword"  
  55.             android:layout_width="fill_parent"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_alignLeft="@+id/tvPasswordName"  
  58.             android:layout_below="@+id/tvPasswordName"  
  59.             android:ems="10"  
  60.             android:inputType="textPassword"  
  61.             android:background="@drawable/background_login_div_bg"  
  62.             android:password="true"  
  63.             android:singleLine="true" />  
  64.         //登录事件  
  65.         <Button  
  66.             android:id="@+id/btnName"  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值