零基础学安卓编程

安卓开发入门教程-数据存储_SharedPreferences

2020-07-24  本文已影响0人  蓝不蓝编程

关注 安卓007 ,免费获取全套安卓开发学习资料

什么是SharedPreferences

SharedPreferences是一种以键值对形式保存数据的存储方式.每条数据都需要指定一个唯一键名来进行区分.可以存储布尔型、整型、字符串等基础数据类型.其特点为简单、轻量,适合保存少量简单类型的数据,不适合保存大批量或复杂类型的数据.SharedPreferences的实质是xml格式存储数据的文件.

基础样例

1. 写入和读取数据

  1. activity代码
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        writeDataBtn.setOnClickListener { writeSharedPreference() }
        readDataBtn.setOnClickListener { readSharedPreference() }
    }

    /**
     * 像SharedPreference中写入数据
     */
    private fun writeSharedPreference() {
        val spFileName = "test_sp_file"
        val editor = getSharedPreferences(spFileName, Context.MODE_PRIVATE).edit()
        editor.putString("userName", "萝莉")
        editor.putInt("age", 16)
        editor.putBoolean("isBeauty", true)
        editor.apply()
    }

    /**
     * 从SharedPreference中读取数据
     */
    private fun readSharedPreference() {
        val spFileName = "test_sp_file"
        val sp = getSharedPreferences(spFileName, Context.MODE_PRIVATE)
        val name = sp.getString("userName", "")
        val age = sp.getInt("age", 0)
        val isBeauty = sp.getBoolean("isBeauty", true)
        Log.d("MainActivity", "姓名:$name")
        Log.d("MainActivity", "年龄:$age")
        Log.d("MainActivity", "是否漂亮:$isBeauty")
    }
}

代码说明:

  1. 对应页面布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <Button
        android:id="@+id/writeDataBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入数据" />
    <Button
        android:id="@+id/readDataBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取数据" />
</LinearLayout>

窥探内幕

SharedPreferences实质为xml文件,其存储路径在/data/data/应用包名/shared_prefs目录下.
本文对应工程生成的文件位于:/data/data/cn.cxy.demo/shared_prefs


可以看到文件名test_sp_file.xml就来自于代码中指定的名称:val spFileName = "test_sp_file"
文件内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <boolean name="isBeauty" value="true" />
    <string name="userName">萝莉</string>
    <int name="age" value="16" />
</map>

备注:
刚安装app后,如果还没有往SharedPreferences写入过数据,那么shared_prefs目录就还不存在.

常用函数说明

函数名 用途
putString 存储字符串类型数据
putInt 存储整型数据
putLong 存储长整型数据
putFloat 存储浮点型数据
putBoolean 存储布尔值,true或false
putStringSet 存储字符串集合
remove 删除指定键名对应的数据记录
clear 清空所有存储的数据
apply 修改数据后,提交保存到文件中
getString 以字符串类型读取出数据
getInt 以整型读取出数据
getLong 以长整型读取出数据
getFloat 以浮点型读取出数据
getBoolean 以布尔值读取出数据
contains 判断是否包含某个键名的数据

完整源代码

https://gitee.com/cxyzy1/SharedPreferenceDemo


安卓开发入门教程系列汇总

开发语言学习

Kotlin语言基础

UI控件学习系列

UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar

关注头条号,第一时间获取最新文章:


上一篇 下一篇

猜你喜欢

热点阅读