温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android学习之SharedPerference存储详解

发布时间:2020-08-20 20:52:07 来源:脚本之家 阅读:100 作者:天秤心已随风去 栏目:移动开发

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI