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. Working with Numbers in PL/SQL(在PL/SQL中使用数字)

    This article gives you all the information you need in order to begin working with numbers in your P ...

  2. android面试题之四

    十六.Android中Dalvik和JVM的区别是什么? 1. Dalvik基于寄存器,而JVM基于栈.基于寄存器的虚拟机对于更大的程序来说,在它们编译的时候,花费的时间更短. 2. Dalvik负责 ...

  3. [Regex Expression] Match mutli-line number, number range

    /^-?\d{,}\.\d+$/gm

  4. 利用boost获取时间并格式化

    利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题. 1. 输出YYYYMMDD #include <boost/date_time/gregorian/gregorian.hpp& ...

  5. C#操作Excel总结

    0. 导入命名空间:  1 2 3 4 using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System. ...

  6. JavaScript当中的eval函数

    eval函数 eval函数接收一个由JavaScript语句组成的字符串,并且返回字符串中最后一条语句的返回值,如果最后一条语句没有返回值,那么eval函数返回undefined.如果传递给eval函 ...

  7. Android网络连接的两种方法:apache client和httpurlconnection的比较

    另见http://blog.csdn.net/mingli198611/article/details/8766585 在官方blog中,android工程师谈到了如何去选择apache client ...

  8. java设计模式之Proxy(代理模式)

    java设计模式之Proxy(代理模式) 2008-03-25 20:30 227人阅读 评论(0) 收藏 举报 设计模式javaauthorizationpermissionsstringclass ...

  9. Android 播放gif图片

    Android的原生控件并不支持播放GIF格式的图片.我们都知道,在Android中如果想要显示一张图片,可以借助ImageView控件来完成,但是如果将一张GIF图片设置到ImageView里,它只 ...

  10. deepin 2014 安装后 ,grub出错

    今天deepin2013一直出错,就想尝试下2014,so,果断下载安装,然后悲剧的又被坑了. 环境win7位于sda,deepin安装在sdb 安装完毕后,启动报错,找不到设备uuid 无奈之下,重 ...