写在前面

在学习完之前的内容,发现没有对组件中的输入的数据进行存储。所以,在这里会提到数据存储。可是输出存储有很多种,上面的 SharedPreferences 就是其中的一个、还有使用数据库(Mysql、sqlite)以及使用文件的形式保存文件。

其实 SharedPreferences 保存的方式,它也是用文件保存。只是它可是用 xml 文件来保存的,一般这个文件我们是无法看到的。而且由于现在的安卓手机的安全性增强,那些处于系统目录下的文件都不能直接去查看(其实也没必要去看)。

SharedPreference 的使用场景非常广泛。可以保存该应用相关信息,以及 QQ 和 微信中的免密登录等等……,不过它的用法也非常的简单。咱们现在开始吧

实例

本实例中声明了 4 个组件,分别是一个可编辑的文本框,两个按钮和一个显示文字的组件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容"/>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="保存"/>

    <Button
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

</LinearLayout>

除了声明这些组件之外,还要声明两个对象:

  • SharedPreferences
  • SharedPreferences.Editor

SharedPreferences 对象用来加载自定义的 xml 文件,通过 getSharedPreferences 方法来获取这个对象。
如果要编辑这个配置文件,则需要调用 getSharedPreferences 中的 editor 方法即可。

完整的 java 代码

package top.bestguo.androidlayout.datastorage;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import top.bestguo.androidlayout.R;

public class SharedPreferenceActivity extends AppCompatActivity {

    // 基本组件声明
    private EditText etName;
    private Button btnSave, btnShow;
    private TextView tvShow;

    // SharedPreferences 对象声明
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor sEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preference);
        etName = (EditText) findViewById(R.id.et_name);
        btnSave = (Button) findViewById(R.id.btn_save);
        btnShow = (Button) findViewById(R.id.btn_show);
        tvShow = (TextView) findViewById(R.id.tv_show);

        // 获取 SharedPreferences 对象
        // 文件名称,直接使用 MODE_PRIVATE
        sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        // 创建 Editor 对象,用于编辑我们创建的 data.xml 配置文件
        sEditor = sharedPreferences.edit();

        // 写文件
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 写入数据
                sEditor.putString("name", etName.getText().toString());
                // 提交数据,提交数据有两种方式,一个是 commit,一个是 apply
                sEditor.apply();
            }
        });

        // 读文件
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 读取数据,将数据展示到 TextView 中
                tvShow.setText(sharedPreferences.getString("name", ""));
            }
        });
    }
}

运行效果如下

后记

这个 xml 文件是保存在你的安卓设备中或者安卓模拟器中,而不是在你的电脑中。默认的安装位置是在 “/data/data//shared_prefs” 中,这个路径在模拟器中是可以看到了,如果我们装在实体机上,就不一定能看到了(感觉市面上大部分的安卓手机都是不能看的吧😊😊😊)

项目的代码都在 AndroidStudy 的 github 仓库中。