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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM