SharePreference的使用

SharePreference

一般用于保存偏好设置,比如说我们设置里的条目

sharepreference使用步骤

1、拿到sharepreference

//拿到share preference
        setting_info = this.getSharedPreferences("setting_info", MODE_PRIVATE);

这里的this是指上下文Context,在Activity中,因为Activity直接或间接继承了Context,所以直接使用this。

2、进入编辑模式

//拿到编辑器
        SharedPreferences.Editor edit = setting_info.edit();

3、保存数据

//保存数据
        edit.putBoolean("state",isChecked);

保存数据时,根据数据的类型boolean,String,float,等等

4、提交数据编辑器

//提交编辑器
        edit.commit();

将其打印到桌面

 

 sharepreference同样属于内部存储,与files/cache相同,在data/data包名下shared_prefs以xml文件形式保存。

它的内容保存都是以键值对的方式保存。

sharepreference数据回显

//数据回显
        boolean state = setting_info.getBoolean("state", false);
        mAllowUnknownSourceSwitch.setChecked(state);

将其设为打开

 关闭程序

 再次运行

整体原码

PreferenceDemoActivity.java

package com.example.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 {
    Switch mAllowUnknownSourceSwitch ;
    private static final String TAG="PreferenceDemoActivity";
    private SharedPreferences setting_info;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preference_layout);
        //找到控件
        mAllowUnknownSourceSwitch=this.findViewById(R.id.allow_unknown_source_switch);
        mAllowUnknownSourceSwitch.setOnCheckedChangeListener(this);
        //拿到share preference
        setting_info = this.getSharedPreferences("setting_info", MODE_PRIVATE);
        //数据回显
        boolean state = setting_info.getBoolean("state", false);
        mAllowUnknownSourceSwitch.setChecked(state);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //对数据进行控制台打印
        Log.d(TAG,"current state"+isChecked);
        //拿到编辑器
        SharedPreferences.Editor edit = setting_info.edit();
        //保存数据
        edit.putBoolean("state",isChecked);
        //提交编辑器
        edit.commit();
    }
}

activity_preference_layout.xml

<?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="80dp">

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

        <TextView
            android:layout_width="wrap_content"
            android:text="未知来源"
            android:textSize="20sp"
            android:textColor="@color/colorAccent"
            android:padding="10dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_width="wrap_content"
            android:text="允许安装未知来源的应用"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <Switch
        android:id="@+id/allow_unknown_source_switch"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content"/>
</RelativeLayout>

原文地址:https://www.cnblogs.com/yeyueweiliang/p/12256699.html