Android学习 | 07.SharedPreferences使用场景和用法

SharedPreferences 就是共享参数,是android专属的轻量级存储数据的方式。

适用场景:

  • 简单且孤立的数据。复杂且相关的数据要存储到数据库中。
  • 文本形式的数据。二进制数据要保存到文件中。
  • 需要持久化存储的数据,在APP退出之后,之前保存的数据依然有效。

实际开发中,共享参数经常存储的数据有APP的个性化配置信息、用户使用APP的行为信息、临时需要保存的片段信息等。

案例:

视图:

没啥特殊的,四个线性布局,一个checkbox,一个按钮

<?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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/name"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />
        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/editext_selector"
            android:hint="@string/input_name"
            android:inputType="text"
            android:maxLength="6"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/age"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="@string/input_age"
            android:inputType="number"
            android:maxLength="6"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"
            android:textSize="@dimen/common_font_size" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/height"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="@string/input_height"
            android:inputType="number"
            android:maxLength="6"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"
            android:textSize="@dimen/common_font_size" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/weight"
            android:gravity="center"
            android:textSize="@dimen/common_font_size"
            android:textColor="@color/black" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/editext_selector"
            android:hint="@string/input_weight"
            android:inputType="number"
            android:maxLength="6"
            android:textColor="@color/black"
            android:textColorHint="@color/grey"
            android:textSize="@dimen/common_font_size" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/ck_married"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/married"
        android:textSize="@dimen/common_font_size"/>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/save_share"
        android:textColor="@color/black"
        android:textSize="@dimen/common_font_size" />

</LinearLayout>

对应java文件:

  • 获取组件的id
  • 给保存按钮创建一个点击方法,用来获取编辑框里的值,用SharedPreferences.Editor使用共享参数的编辑器,这样可以对每一项进行编辑,最后commit提交一下
  • preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
  • Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。
  • reload()方法重新加载一下保存的数据。
package com.example.chapter06;

import androidx.appcompat.app.AppCompatActivity;

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

public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;
    private SharedPreferences preferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_write);

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);

        findViewById(R.id.btn_save).setOnClickListener(this);

        preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        reload();
    }

    private void reload() {
        String name = preferences.getString("name", null);
        if (name != null) {
            et_name.setText(name);
        }

        int age = preferences.getInt("age", 0);
        if (age != 0) {
            et_age.setText(String.valueOf(age));
        }

        float height = preferences.getFloat("height", 0f);
        if (height != 0f) {
            et_height.setText(String.valueOf(height));
        }

        float weight = preferences.getFloat("weight", 0f);
        if (weight != 0f) {
            et_weight.setText(String.valueOf(weight));
        }

        boolean married = preferences.getBoolean("married", false);
        ck_married.setChecked(married);
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name",name);
        editor.putInt("age",Integer.parseInt(age));
        editor.putFloat("height",Float.parseFloat(height));
        editor.putFloat("weight",Float.parseFloat(weight));
        editor.putBoolean("married", ck_married.isChecked());
        editor.commit();

    }
}

只要我们关掉后台之后重新打开程序数据还有的话就成功啦 !

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: android.content.SharedPreferencesAndroid中的一个API,用于为应用程序存储和读取键值对形式的数据。它提供了一种轻量级的数据存储方式,适用于存储一些简单的配置信息或用户偏好设置。 android.content.Context.getSharedPreferences()是一个Context类中的方法,用于获取一个SharedPreferences对象。Context是Android中很重要的一个类,代表当前应用程序的运行环境。通过Context的getSharedPreferences()方法,我们可以获取到应用程序的默认SharedPreferences,也可以通过指定名称获取到不同的SharedPreferences使用SharedPreferences可以方便地进行数据的存储和读取操作。它通过键值对的方式存储数据,其中键是一个唯一标识,值可以是各种基本类型的数据,如字符串、整数、布尔值等。可以通过调用SharedPreferences的edit()方法获取到一个SharedPreferences.Editor对象,通过这个Editor对象可以进行数据的写入操作。可以使用putXXX()方法将数据存储到SharedPreferences中,并通过commit()方法将修改提交保存。另外,还可以通过SharedPreferences的getXXX()方法读取已经存储的数据。 SharedPreferences适用于存储一些简单的配置信息或用户偏好设置,其存储方式是以XML文件的形式保存在应用程序的/data/data/<package name>/shared_prefs/目录下。可以通过SharedPreferences的getDefaultSharedPreferences()方法获取默认的SharedPreferences对象,它的名称是根据应用程序的包名来自动生成的。 使用SharedPreferences可以方便地进行数据的读写操作,特别适合用于存储一些简单的应用配置或用户设置。但是对于需要存储大量复杂数据结构的情况,或者需要频繁读写数据的场景,可能不太适合使用SharedPreferences,可以考虑使用其他更合适的数据存储方式,如数据库。 ### 回答2: android.content.SharedPreferencesandroid.content.Context.getSharedPreferences都是Android中用于实现数据持久化的工具类和方法。 android.content.SharedPreferences是一个接口,用于存储简单键值对数据。它提供了一种轻量级的存储方式,可以非常方便地存储和读取数据。SharedPreferences可以实现应用程序的配置信息存储,用户偏好设置的存储等功能。它的底层实现是基于XML文件的,数据存储在应用程序的沙盒目录中。 android.content.Context.getSharedPreferences是Context类的一个方法,用于获取SharedPreferences对象。Context是一个Android中的核心类,代表了应用程序的环境信息。在Android应用程序的各个组件中都可以通过Context对象获取应用程序的资源、启动新的组件、发送广播等等。通过Context.getSharedPreferences方法,我们可以获取一个SharedPreferences对象,并利用这个对象进行数据的存储和读取。 总结起来,android.content.SharedPreferencesandroid.content.Context.getSharedPreferences都是用于实现数据持久化的工具。SharedPreferences是数据存储的接口,它提供了简单的存储方式,可以存储键值对数据;而Context.getSharedPreferences是获取SharedPreferences对象的方法,通过Context类的实例获取SharedPreferences对象,方便进行数据的存储和读取。这两个工具的使用可以帮助开发者实现应用程序的配置数据存储、用户偏好设置的存储等功能。 ### 回答3: android.content.SharedPreferencesAndroid平台上用于存储和读取轻量级的键值对数据的类。它是一种基于XML文件的存储机制,可用于存储应用程序的相关配置信息、用户偏好设置等。 首先,我们需要获取SharedPreferences对象,这可以通过调用Context类的getSharedPreferences方法来实现。这个方法需要传入两个参数,一个是存储文件的名称,另一个是操作模式。 getSharedPreferences方法返回一个SharedPreferences对象,我们可以通过这个对象进行数据的读取和写入。其中,存储文件的名称是一个用于标识数据的关键词,它对应着一个XML文件,用来存储键值对数据。操作模式有多种选项,如MODE_PRIVATE、MODE_MULTI_PROCESS等,用于指定多个应用程序共享数据的方式。 一旦我们获取到了SharedPreferences对象,就可以通过它的edit方法获取一个Editor对象,从而可以进行数据的编辑操作。Editor对象提供了一系列的方法,如putBoolean、putInt、putString等,用于向SharedPreferences中写入数据。 另外,在读取数据时,我们可以通过SharedPreferences对象的getBoolean、getInt、getString等方法获取存储的数据。 总之,SharedPreferences提供了一个简单而方便的存储数据的方式,适用于各种应用场景。不过需要注意的是,SharedPreferences适合存储少量的简单数据,对于大量数据的存储,建议使用其他的存储方式,如数据库。 通过android.content.SharedPreferencesandroid.content.Context.getSharedPreferences方法,我们可以实现方便的存储和读取数据的功能,从而更好地满足应用程序的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值