Android学习之SharedPerference存储详解

时间:2022-07-28
本文章向大家介绍Android学习之SharedPerference存储详解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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 

以上就是本文的全部内容,希望对大家的学习有所帮助。