安卓开发新手教程 SharedPreferences

372 阅读2分钟

SharedPreference(简称SP)是Android上一种非常易用的轻量级存储方式。SP采用key-value(键值对)形式来存储数据,最终以xml格式的文件来持久化到存储介质上。默认的存储位置在/data/data/<包名>/shared_prefs目录下,当用户卸载此应用程序或者清除应用数据时,SP保存的数据会一并清除。接下来就让我们看看如何使用SharedPreference并搭建一个简单的存储项目.

使用场景

  • 目前我所使用的场景主要是简单登录页面的使用.
  • 但其实关于存储的许多方面都可以使用.

接下来我们就先搭建一个简单的框架

PixPin_2024-12-22_11-13-07.png

  • 接下来就是xml的代码
<EditText
    android:id="@+id/et_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入你要存储的值"
    android:layout_marginTop="30dp" />
<Button
    android:id="@+id/BOTTOM_SAVE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="存储"
    android:textSize="20dp"
    android:onClick="save"/>
<Button
    android:id="@+id/BOTTOM_GET"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="获取"
    android:textSize="20dp"
    android:onClick="get"/>

我们就可以开始关于主页面的代码构建

  • 首先就是要绑定输入框
etContent=findViewById(R.id.et_content);//使用findViewById可以绑定输入框
  • 接下来就要获取到输入框里的值
String content=etContent.getText().toString();

基本的框架搭建好了我们就可以开始SharedPreference的使用了

SharedPreference的写入操作

SharedPreferences spSave = getSharedPreferences("spSave", MODE_PRIVATE);
SharedPreferences.Editor edit = spSave.edit();
edit.putString("content",content);
edit.apply();
  • getSharedPreferences创建一个对象用来存储
  • spSave.edit();就是用来可以写入的一种方法
  • edit.putString("content",content);采用键值对的方法来存入获取的content
  • edit.apply();就是写入这个动作,且与commit比速度更快

SharedPreference的输出操作

SharedPreferences spSave = getSharedPreferences("spSave", MODE_PRIVATE);
String content = spSave.getString("content", "");
etContent.setText(content);
  • 同样的先写一个getSharedPreferences
  • 之后利用getString获取其中的值
  • 最后setText到文本框内

最后附上整个项目的代码

package com.example.sharedpreferencedemo;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etContent=findViewById(R.id.et_content);
    }

    public void save(View view) {
        String content=etContent.getText().toString();
        SharedPreferences spSave = getSharedPreferences("spSave", MODE_PRIVATE);
        SharedPreferences.Editor edit = spSave.edit();
        edit.putString("content",content);
        edit.apply();
        Toast.makeText(this,"已保存"+content,Toast.LENGTH_SHORT).show();
        etContent.setText("");

    }

    public void get(View view) {
        SharedPreferences spSave = getSharedPreferences("spSave", MODE_PRIVATE);
        String content = spSave.getString("content", "");
        etContent.setText(content);
        etContent.setSelection(content.length());
    }
}