SharePreference是用于保存数据用的。主要调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个參数是文件名。第二个參数是操作模式。

操作模式有三种:



MODE_PRIVATE(私有) 

MODE_WORLD_READABLE(可读)

MODE_WORLD_WRITEABLE(可写)



SharePreference提供了获得数据的方法。如getString(String key,String defValue)等。调用harePreferences的edit()方法返回SharePreferences.Editor内部接口。该接口提供了保存数据的方法如:putString(String
key,String value)等,调用该接口的commit()方法能够将数据保存。



效果图例如以下:





主要xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_login_activity"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="35dip"
android:layout_marginTop="150dip"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20dp" /> <EditText
android:id="@+id/username"
android:layout_width="200dp"
android:layout_height="35dp"
android:background="@drawable/bg_input_center" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="35dip"
android:layout_marginTop="8dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 密码:"
android:textSize="20dp" /> <EditText
android:id="@+id/password"
android:layout_width="200dp"
android:layout_height="35dp"
android:background="@drawable/bg_input_center"
android:password="true" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="75dip"
android:layout_marginTop="8dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码:" /> <CheckBox
android:id="@+id/savePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> <Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dip"
android:text="登陆" /> </LinearLayout> </LinearLayout>

保存数据到文件的主要函数:

public void setUserInfo(String key, String value) {
SharedPreferences sp = context.getSharedPreferences(USER_INFO,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
editor.putString(key, value);
26 editor.commit();
27 }

sharepreference实现记住password功能的更多相关文章

  1. java实现记住密码功能(利用cookie)

    <br> <input type="text" id="userName" name="userName" value=& ...

  2. jquery.cookie.js 操作cookie实现记住密码功能的实现代码

    jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下.   复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...

  3. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

  4. android: SharedPreferences实现记住密码功能

    既然是实现记住密码的功能,那么我们就不需要从头去写了,因为在上一章中的最佳实 践部分已经编写过一个登录界面了,有可以重用的代码为什么不用呢?那就首先打开 BroadcastBestPractice 项 ...

  5. asp.net记住我功能

    登录页面的记住我功能   不能用session的原因:sessionID是以cookie的形式存在浏览器端的内存中  如果用户把浏览器关闭 则sessionID就消失     但是服务器端的sessi ...

  6. vue项目实现记住密码功能

     一.谷歌浏览的残留问题 现在很多的网站都有一个需求是记住密码这个功能,为的是避免用户下次登录的时候繁琐的输入过程. 像是一些主流的浏览器(比如Chrome)都有了这个功能,而且如果你登录了Chrom ...

  7. js中记住密码功能

    js中记住密码功能(在前端实现) 直接上例子(如果你也要实现的话注意改一些变量名称,jsp代码不包含样式) Jsp代码: <form class="am-form tpl-form-l ...

  8. js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,代码如下: //设置cookie var passKey = '4c05c54d952b11e691d76c0b843ea7f9'; function setCookie( ...

  9. 危急,不要任意让站点记住password自己主动登陆!

    为了方便用户登录,差点儿全部的站点都实现了"记住password"."自己主动登陆"这样似乎人性化的功能. 我也非常喜欢这个功能,由于我自己的脑子实在是讨厌记东 ...

随机推荐

  1. Rainmeter 雨滴桌面 主题分享

    说明 先安装主程序 Rainmeter-3.1.exe,然后安装 Techzero_1.0.rmskin,打开主题管理应用主题就可以. 下载 http://pan.baidu.com/s/1i3zI3 ...

  2. .net 4.5 新特性 async await 一般处理程序实例

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Sys ...

  3. 实现Android操作系统11种传感器介绍

    在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...

  4. MVC 错误处理1

    实例1. /// <summary> /// 错误处理 /// 404 处理 /// </summary> protected void Application_Error(o ...

  5. 【转载】ADO.NET与ORM的比较(2):NHibernate实现CRUD

    [转载]ADO.NET与ORM的比较(2):NHibernate实现CRUD  转自周公 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibe ...

  6. MVC自我学起之MVCMusic开发中遇到问题:musicstore edit方法出错的原因和解决方法

    错误提示: 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项. 解决案: 1.在view中或model中增加隐藏id 1 ...

  7. 关于uisliderview 监听停止滑动的状态

    今天遇到一个问题,做颜色控制的时候,通过slider 改变颜色的亮度.如果直接在slider 上绑定事件,则改变一次就需要向服务器发送一次请求.这种是显然不合理的. 所以使用了下面的解决方法 先将sl ...

  8. C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)

    namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...

  9. 使用Win32 API 查找文件

    头文件:#include <windows.h> //FindFirstFile() 获得指定目录的第一个文件 HANDLE FindFirstFile( LPCTSTR lpFileNa ...

  10. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...