Android持久化技术之SharedPreferences存储实例详解

2019-12-10 19:18:51丽君
易采站长站为您分析Android持久化技术之SharedPreferences存储,结合实例形式较为详细的分析了SharedPreferences存储的原理、应用及具体实现方法,需要的朋友可以参考下  

本文实例讲述了Android持久化技术之SharedPreferences存储。,具体如下:

1、SharedPreferences存储

在前面一篇文章《Android持久化技术之文件的读取与写入实例详解》中,我们介绍了Android持久化技术的文件的读取与写入。在本文中,继续介绍Android持久化技术另外一个SharedPreferences存储。

(1)SharedPreferences存储方式是基于key-value的,通过key可以找到对应的value。
(2)支持多种数据类型存储,比如字符串、整形、布尔型等,并有对应的存储与获取方法。
(3)获取SharedPreferences对象有多种方式。
使用Context类的getSharedPreferences方法。
使用Activity类的getPreferences方法
使用PreferenceManager类的getDefaultSharedPreferences方法
(4)当存储时,需要通过SharedPreferences对象获取SharedPreferences.Editor对象
(5)默认存储路径为:/data/data/包名/shared_prefs/目录
(6)存储文件类型为xml文件

2、示例

场景:点击保存按钮,存储数据;点击恢复按钮,恢复数据。

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:stretchColumns="1"
  >
  <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Account:" />
    <EditText
      android:id="@+id/account"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="Input your account here"
      android:ems="10" >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Password:"
       />
    <EditText
      android:id="@+id/password"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPassword"
      >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/login"
    android:layout_span="2"
    android:layout_height="wrap_content"
    android:text="save data" />
  </TableRow>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:background="#ff0000"
    android:text="我是万恶的分割线"
    android:textSize="20sp"
    android:gravity="center"
    />
   <TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Account:" />
    <EditText
      android:id="@+id/account2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10" >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Password:"
       />
    <EditText
      android:id="@+id/password2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPassword"
      >
    </EditText>
  </TableRow>
  <TableRow
    android:id="@+id/tableRow6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
    android:id="@+id/login2"
    android:layout_span="2"
    android:layout_height="wrap_content"
    android:text="restore data" />
  </TableRow>
</TableLayout>