当前位置: 移动技术网 > 移动技术>移动开发>Android > Android持久化技术之SharedPreferences存储实例详解

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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://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

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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网