日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?SharedPreference存儲文件三步走_Android

作者:知奕奕 ? 更新時間: 2023-02-14 編程語言

SharedPreference

他的使用方法非常簡單,不夸張的說,僅需要一個 getSharedPreferences 就可以完成大部分操作

概念與權限

SharedPreference 存儲文件的位置在:data/data/你的工程包名/shared_prefs

getSharedPreferences 的第二個參數需要傳入一個操作模式,目前僅剩下 MODE_PRIVATE 這一個可選,他表示僅當前 app 可以操作此 SharedPreference

存儲數據

存儲三步走:

  • 使用 getSharedPreferences.edit() 獲取一個 Editor 對象
  • 使用諸如 putString 方法,按照數據類型并以鍵值對的形式插入數據
  • 最后使用 apply() 保存修改

方法一:實例化 editor 后按步驟執行

val editor = getSharedPreferences("data",Context.MODE_PRIVATE).edit()
editor.putString("name","jack")
editor.apply()

方法二:直接 lambda 解決,免去 apply

getSharedPreferences("data", Context.MODE_PRIVATE).edit {
    putString("name", "Tom")
    putInt("age", 28)
    putBoolean("married", false)
}

獲取數據

這玩意就更簡單了,直接 getSharedPreferences 獲取存儲文件,然后按照 key 拿到 value 就好了

val prefs = getSharedPreferences("data", Context.MODE_PRIVATE)
val name = prefs.getString("name", "")
val age = prefs.getInt("age", 0)
val married = prefs.getBoolean("married", false)
Log.d("MainActivity", "name is $name")
Log.d("MainActivity", "age is $age")
Log.d("MainActivity", "married is $married")

簡單存儲案例

設置存取按鈕

在 mainactivity 的布局文件中,我們添加倆按鈕,一個存東西,一個取東西;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button
        android:id="@+id/saveButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save Data"
        />
    <Button
        android:id="@+id/restoreButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Restore Data"
        />
</LinearLayout>

主代碼

寫在 MainAcitvity.kt

package com.zhiyiyi.listviewdemo
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.content.edit
import kotlinx.android.synthetic.main.activity_main.*
import java.io.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        saveButton.setOnClickListener {
            getSharedPreferences("data", Context.MODE_PRIVATE).edit {
                putString("name", "Tom")
                putInt("age", 28)
                putBoolean("married", false)
            }
        }
        restoreButton.setOnClickListener {
            val prefs = getSharedPreferences("data", Context.MODE_PRIVATE)
            val name = prefs.getString("name", "")
            val age = prefs.getInt("age", 0)
            val married = prefs.getBoolean("married", false)
            Log.d("MainActivity", "name is $name")
            Log.d("MainActivity", "age is $age")
            Log.d("MainActivity", "married is $married")
        }
    }
}

原文鏈接:https://blog.csdn.net/delete_you/article/details/127186812

欄目分類
最近更新