当前位置:首页 > 移动开发 > 正文

Android开发数据持久化存储,sharePreference(偏好选择)的练习

2024-03-31 移动开发

一、目标

以编写手机设置界面为例子,学习sharePreference的使用

二、具体内容

sharePreference主要用于储存用户的偏好设置,例如在安装未知应用的时候是否提醒等等,一般是那种选择的按钮。

储存其步骤共有4步:(假设layout界面的switch id为is_allow_unkown_apps_switch)

//第一步、拿到sharePreference

  sharedPreferences = this.getSharedPreferences("set_info_true", MODE_PRIVATE);

//第二步、进入编辑模式

  SharedPreferences.Editor edit = sharedPreferences.edit();

//第三步、保存数据

  edit.putBoolean("state",isChecked);

//第四步、保存编辑器

  edit.commit();

//实现回写

boolean state = sharedPreferences.getBoolean("state", false);//用state来接受存好的数据,默认为false
isAllowUnkownSouce.setChecked(state);//回写

三、源代码

package com.example.qq_logindemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.Nullable;

public class PreferenceDemoActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

    private Switch isAllowUnkownSouce;
    private static final String TAG="PreferenceDemoActivity";
    SharedPreferences sharedPreferences;
    //private static final String TAG="PreferenceDemoActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preference_demo);
        //拿到控件
        initView();
        //设置监听
        initClickListener();

        //第一步、拿到sharedPreferences
        sharedPreferences = this.getSharedPreferences("set_info_true", MODE_PRIVATE);

        boolean state = sharedPreferences.getBoolean("state", false);
        isAllowUnkownSouce.setChecked(state);


    }

    private void initClickListener() {
        isAllowUnkownSouce.setOnCheckedChangeListener(this);
    }

    private void initView() {
        isAllowUnkownSouce = this.findViewById(R.id.is_allow_unkown_apps_switch);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //在这里对数据进行保存
        //Log.d(TAG, "onCheckedChanged: "  isChecked);

        //第二步、进入编辑模式
        SharedPreferences.Editor edit = sharedPreferences.edit();
        //第三步、保存数据
        edit.putBoolean("state",isChecked);
        //第四步、保存编辑器
        edit.commit();
    }
}
PreferenceDemoActivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="未知来源"
            android:textColor="@color/colorAccent"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:padding="10dp"
            android:text="是否允许未知应用的安装" />


    </LinearLayout>

    <Switch
        android:id="@ id/is_allow_unkown_apps_switch"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"/>
</RelativeLayout>
View Code

四、运行结果

 

如果这个界面被关闭之后再次打开,显示的还会是之前的选择结果。

 

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/yidong/10979.html