Titan笔记

  • 首页
  • Java
  • 数据结构
  • C语言
  • Web
  • 杂谈
  • 移动开发
  • 逸笔挥墨
Titan笔记
分享学习,研究与开发的点滴记忆
  1. 首页
  2. 移动开发
  3. Android
  4. 正文

Android数据存储方式之SharedPreferences

2021年7月27日 961点热度 13人点赞 0条评论

不同于文件的存储方式,SharedPreferences提供了一种K-V键值对的数据存储方式。

也就是说,当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可以通过这个键把相应的值取出来。

而且SharedPreferences还支持多种不同的数据类型存储,如果存储的数据类型是整型,那么读取出来的数据也是整型的;如果存储的数据是一个字符串,那么读取出来的数据仍然是字符串。

实际上,SharedPreferences将保存于APP数据目录下的xml文件中,也就是以XML的格式来保存的。显然,SharedPreferences只能保存不太敏感的明文,或者采取一些加密的手段来加密数据后再存储。

下面是SharedPreferences进行数据读写的示例:

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".SharedPreferenceActivity">

    <EditText
        android:id="@+id/sharedPreferenceEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp" />

    <Button
        android:id="@+id/sharedPreferenceWriteButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="写入数据" />

    <Button
        android:id="@+id/sharedPreferenceReadButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据" />
</LinearLayout>

Activity类文件

class SharedPreferenceActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_shared_preference)
        val editText = findViewById<EditText>(R.id.sharedPreferenceEditText)
        findViewById<Button>(R.id.sharedPreferenceWriteButton).setOnClickListener {
            val text = editText.text.toString()
            savePreferenceData("text", text)
        }
        findViewById<Button>(R.id.sharedPreferenceReadButton).setOnClickListener {
            val text = readPreferenceData("text")
            editText.setText(text)
        }

    }

    fun savePreferenceData(key: String, value: String) {
        val sharedPreference = getSharedPreferences("data", MODE_PRIVATE)
        sharedPreference.edit().apply {
            putString(key, value)
            apply()
        }
    }

    fun readPreferenceData(key: String): String? {
        val sharedPreference = getSharedPreferences("data", MODE_PRIVATE)
        return sharedPreference.getString(key, "")
    }
}

总结

可以看到,我们通过 getSharedPreferences 来获取一个 SharedPreferences 实例,用于读写;在写操作中,通过调用 SharedPreferences 的 edit() 方法获取一个Editor实例,用于编辑SharedPreferences的键值(在SharedPreference的源码注释中这样说明:allowing you to modify the values in this SharedPreferences object.),然后通过putXXX来保存XXX类型的数据,如putString,最后apply()来完成更改;在读操作中,直接调用SharedPreferences的getXXX获取值数据即可。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: AndroidSharedPreference Android数据存储 SharedPreference SharedPreference数据存储
最后更新:2021年7月27日

Titan

不为岁月流逝蹉跎,不为潮流的势头去附和

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论
最新 热点 随机
最新 热点 随机
Docker配置IPv6容器网络支持 什么是Elastic Stack,ELK的发展历程 K8s中Pod的基本概念 云原生 - 浅谈容器基础与K8S架构设计 腾讯Serverless体验,使用TypeScript编写并部署云函数 Go-Proxy-Checker,一款基于Go编写的高性能代理服务器验证工具
[Web] CSS 中 Display(显示) 与 Visibility(可见性)的区别与用法 [数据结构] 两个有序线性表的合并 [数据结构]链式存储: 多项式求和 Go-Proxy-Checker,一款基于Go编写的高性能代理服务器验证工具 [数据结构] 队列的链式存储实现 Spring Cloud 微服务学习笔记 - 负载均衡服务调用
分类
  • Android
  • C语言
  • Elasticsearch
  • Hadoop
  • Hive
  • Java
  • JavaWeb
  • Kubernetes
  • Linux运维之道
  • Mybatis学习笔记
  • Python
  • SpringCloud
  • Web
  • Web前端
  • Web后端
  • 云原生
  • 并发编程
  • 开发工具
  • 数据库
  • 数据结构
  • 杂谈
  • 移动开发
  • 移动测试
  • 诗词歌赋
  • 软件测试
  • 逸笔挥墨
  • 随摘
标签聚合
二叉树 数据结构 Java Mybatis学习笔记 Python 链式存储 Apache-Hive JavaWeb

COPYRIGHT © 2013-2021 Titan. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备20001822号-1

豫公网安备 41010502004418号