当前位置: 移动技术网 > 移动技术>移动开发>Android > Android学习之SharedPerference存储详解

Android学习之SharedPerference存储详解

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

sharedperference不同同于文件存储,它是使用键值的方式来存储数据,对于保存的每一条数据都会给一个键值,这样在读取数据时直接通过键值取出相应数据。amdroid提供了三个方法来获取实例:

1.context类中的getsharepreferences()方法

它接收两个参数,第一个是文件名;第二个是操作模式,目前只有mode_private可选,这是默认的操作模式,表示只有当前的应用可以对文件进行操作。

2.activity类中的getpreference()方法

它只接收一个操作模式参数,因为使用这个方法会自动将类名sharedpreference作为文件名。

3.preferencemanager类中的getdefaultsharedpreference()方法

主要由三步来实现:

  (1)调用sharedpreferences对象的edit()方法来获取一个sharedpreferences.editor对象。
  (2)向sharedpreferences.editor对象中添加数据,比如添加一个布尔型数据就使用putboolean()方法,依次论推。
  (3)调用apply()方法将添加的数据提交,从而完成数据操作。`

使用案例

mainactivity:

package com.example.sharedpreferences;

import android.content.sharedpreferences;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.toast;

public class mainactivity extends appcompatactivity implements view.onclicklistener{

  private button button;
  private button restore_btn;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    button = (button)findviewbyid(r.id.save_data);
    button.setonclicklistener(this);
    restore_btn = (button)findviewbyid(r.id.restore_data);
    restore_btn.setonclicklistener(this);
  }

  @override
  public void onclick(view view) {
    switch (view.getid()){
      case r.id.save_data:
        savedata();
        toast.maketext(mainactivity.this,"保存成功!",toast.length_short).show();
        break;
      case r.id.restore_data:
        restordata();
        toast.maketext(mainactivity.this,"恢复成功",toast.length_short).show();
        break;
      default:
        break;
    }
  }
  public void savedata(){
    sharedpreferences.editor editor = getsharedpreferences("data",mode_private).edit();
    editor.putstring("name","tom");
    editor.putint("age",18);
    editor.putboolean("married",false);
    editor.apply();
  }
  public void restordata(){
    sharedpreferences preferences = getsharedpreferences("data",mode_private);
    string name = preferences.getstring("name","");
    int age = preferences.getint("age",0);
    boolean married = preferences.getboolean("marred",false);
    log.d("主活动: ","获取到名字: "+name);
    log.d("主活动: ","获取到年龄: "+age);
    log.d("主活动: ","获取到婚配: "+married);
  }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.sharedpreferences.mainactivity">

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/save_data"
      android:text="保存数据"/>
    <button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/restore_data"
      android:text="恢复数据"/>
  </linearlayout>

</android.support.constraint.constraintlayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

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

相关文章:

验证码:
移动技术网