nt defValue):获取Int型数据
getLong(String key, long defValue):获取Long型数据
getString(String key, String defValue):获取String型数据
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener):
注册一个当preference发生改变时被调用的回调函数。
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener):删除当前回调函数。
三、重要接口SharedPreferences.Editor
1.简介
用于修改SharedPreferences对象的内容,所有更改都是在编辑器所做的批处理,而不是复制回原来的SharedPreferences或持久化存储,直到你调用commit(),才将持久化存储。
2.重要方法
clear():清除内容。
commit():提交修改
remove(String key):删除preference
下面通过"记住密码"功能
四、实例
效果图如下
Android简单数据存储类SharedPreferences详解及实例
首页
Android简单数据存储类SharedPreferences详解及实例
登录成功后的页面
Android简单数据存储类SharedPreferences详解及实例
当第一次登录点击"记住密码"后,第二次打开时的页面
2.代码
布局文件 login.xml
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="right" android:layout_gravity="right"
android:background="@drawable/default_bg" android:orientation="vertical">
< TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
< TableRow android:gravity="center" android:layout_gravity="center">
< ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/ivlogo"
>
< /ImageView>
< /TableRow>
< /TableLayout>
< TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
< TableRow android:layout_marginTop="100dip">
< TextView android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:gravity="center_vertical"
android:layout_height="wrap_content" android:id="@+id/tvaccount"
android:text="帐号:" android:textSize="20sp">
< /TextView>
< EditText android:layou