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 }

最新文章

  1. 基于本地存储的kvm虚拟机在线迁移
  2. SQL Server数字辅助表的实现
  3. selenium读取txt文件的几种方式
  4. bower 基本应用
  5. Oracle的DDL、DML、DCL
  6. [luogu P2170] 选学霸(并查集+dp)
  7. 004. 线程间操作无效: 从不是创建控件“textBox1”的线程访问它
  8. scu 4436: Easy Math 水题
  9. 定位 - CoreLocation - 指南针
  10. 高效的TCP消息发送组件
  11. Cordova VS React Native 谁是未来? - b
  12. 记录GDI 文本的设置
  13. ajax+json数据传输
  14. Query语句对系统性能的影响
  15. java模拟数据库缓存
  16. Head First设计模式之原型模式
  17. 20175310 《Java程序设计》第8周学习总结
  18. 转《service worker在移动端H5项目的应用》
  19. C# cmd bcp 导出数据
  20. MFC限制edit控件的字符输入长度

热门文章

  1. mac下显示隐藏文件
  2. Linux安装中文man手冊
  3. [转] SQL Server游标的使用
  4. SQL server根据值搜表名和字段
  5. JavaScript禁止用户多次提交方法
  6. 漂亮回答面试官struts2的原理
  7. 写一个Windows上的守护进程(3)句柄的管理
  8. hdu1443(约瑟夫环游戏的原理 用链表过的)
  9. iOS UIWebView 访问https 绕过证书验证的方法
  10. html 学习笔记--基础篇