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. sql注入数据库修复方法

    1.第一种情况是 需要将指定的 注入字符串全部替换掉(仅替换注入的字符串为空) declare @delStr nvarchar(500) set @delStr='<script src=ht ...

  2. C++中malloc/free和new/delete 的使用

    malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...

  3. Asp.net开发常用的51个非常实用的代码

    1.弹出对话框.点击转向指定页面 Code: Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</scrip ...

  4. SQL SERVER 自定义函数 split

    Create function [dbo].[split] ( ), ) ) )) as begin declare @i int set @SourceSql=rtrim(ltrim(@Source ...

  5. Android5.0新特性:RecyclerView实现上拉加载更多

    RecyclerView是Android5.0以后推出的新控件,相比于ListView可定制性更大,大有取代ListView之势.下面这篇博客主要来实现RecyclerView的上拉加载更多功能. 基 ...

  6. oracle导出数据显示出现ora-00109或者LRM-00109出错修改办法

    出现这种问题是因为日期格式的问题,调整日期格式后就可以了 改成yyyy-mm-dd的格式就好了

  7. (原)Ubuntu16中卸载并重新安装google的Protocol Buffers

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5782992.html 目前最新的是1.6.1 1. 卸载掉老版本的Protocol: sudo apt ...

  8. No1_4.数组的基本操作_Java学习笔记

    import java.util.Arrays; public class HelloArrayOp { public static void main(String[] args) { // TOD ...

  9. 字符串分割与数组的分割 split()VSsplice()&slice()

    一.作用对象 1.split()方法是对字符串的操作:splice()和slice()是对数组的操作.slice()也可用于字符串. 二.参数 1.split(separator,howmany) 参 ...

  10. sql中NULL的问题

    sql中NULL的问题   今天一不小心在sql中写出以下脚本 select defaultPositionId from TableName where UserId=1100528 and def ...