服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android持久化技术之SharedPreferences存储实例详解

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

2021-05-11 17:08残缺的孤独 Android

这篇文章主要介绍了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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?xml version="1.0" encoding="utf-8"?>
<tablelayout xmlns:android="http://schemas.android.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>

(2)mainactivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.example.testsharedpreferences;
import android.app.activity;
import android.content.sharedpreferences;
import android.os.bundle;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;
/**
 * android 持久化技术-----sharedpreferences存储
 * @author yy
 *
 */
public class mainactivity extends activity {
  private edittext accountedit;
  private edittext passwordedit;
  private button savebutton;
  private button restorebutton;
  private sharedpreferences pref;
  private sharedpreferences.editor editor;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    //存储按钮
    savebutton = (button) findviewbyid(r.id.login);
    //为存储按钮添加点击事件
    savebutton.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view arg0) {
        //获取sharedpreferences对象
        //第一个参数:文件名,没有则新建。第二个参数:写入模式-覆盖
        pref = getsharedpreferences("second", mode_private);
        //获取sharedpreferences.editor对象
        editor = pref.edit();
        //获取输入的账号内容
        accountedit = (edittext) findviewbyid(r.id.account);
        string account = accountedit.gettext().tostring();
        //获取输入的密码内容
        passwordedit = (edittext) findviewbyid(r.id.password);
        string password = passwordedit.gettext().tostring();
        //存储用户名和密码
        editor.putstring("account", account);
        editor.putstring("password", password);
        //提交
        editor.commit();
        toast.maketext(getapplicationcontext(), "保存成功", toast.length_short).show();
      }
    });
    //获取恢复按钮对象
    restorebutton = (button) findviewbyid(r.id.login2);
    //添加事件
    restorebutton.setonclicklistener(new onclicklistener() {
      @override
      public void onclick(view arg0) {
        //获取sharedpreference对象
        pref = getsharedpreferences("second", mode_private);
        //读取内容
        string account = pref.getstring("account", "this is default value");
        string password = pref.getstring("password", "this is default value");
        //设置到响应位置
        edittext edittext2 = (edittext)findviewbyid(r.id.account2);
        edittext2.settext(account);
        edittext passwordtext2 = (edittext) findviewbyid(r.id.password2);
        passwordtext2.settext(password);
        toast.maketext(getapplicationcontext(), "恢复成功", toast.length_short).show();
      }
    });
  }
  @override
  public boolean oncreateoptionsmenu(menu menu) {
    // inflate the menu; this adds items to the action bar if it is present.
    getmenuinflater().inflate(r.menu.main, menu);
    return true;
  }
}

3、结果

输入内容后,当点击“save data”按钮后,存储文件为second.xml,如下:

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

对应内容:

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

下面是效果图:

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

希望本文所述对大家android程序设计有所帮助。

延伸 · 阅读

精彩推荐