• 安卓应用开发中的参数修改保存


    在开发安卓应用中,总是需要保存设置一些参数。开始我找到用SharedPreferences的方法,但官方网站说过期了,新的要用DataStore。本文就是介绍DataStore Preference 的简单编程。

    DataStore是Jetpack中的一个组件,用于做数据持久化,DataStore以异步、一致的事务方式存储数据,克服了SharedPreferences的一些缺点,DataStore基于Kotlin协程和Flow实现,就是用来取代SharedPreferences的。

    DataStore 提供 2 种不同的实现:

    • Preferences DataStore
    • Proto DataStore

    Preferences DataStore 比较简单,本文就只是介绍这种简单方法。

    一、新建工程,添加依赖

    首先在android Studio下新建一个工程,选择 Empty Active View 作为模板,名字取为 DataStore Demo。

    在app模块下的build.gradle中的dependencies{}闭包中添加如下依赖:

    1. //Preferences DataStore
    2. implementation ("androidx.datastore:datastore-preferences:1.0.0")
    3. implementation ("androidx.datastore:datastore-preferences-core:1.0.0")

    二,修改布局文件activity_main.xml

    这个文件在res/layout 下面,文件的内容如下:

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:gravity="center"
    8. android:orientation="vertical"
    9. tools:context=".MainActivity">
    10. <TextView
    11. android:id="@+id/textView"
    12. android:layout_width="wrap_content"
    13. android:layout_height="wrap_content"
    14. android:text="Hello World!" />
    15. <EditText
    16. android:id="@+id/number"
    17. android:layout_width="150dp"
    18. android:layout_height="wrap_content"
    19. android:inputType="number"
    20. android:text=""
    21. android:hint="input number"
    22. />
    23. <Button
    24. android:id="@+id/btn_put"
    25. android:layout_width="match_parent"
    26. android:layout_height="wrap_content"
    27. android:text="Save" />
    28. <Button
    29. android:id="@+id/btn_get"
    30. android:layout_width="match_parent"
    31. android:layout_height="wrap_content"
    32. android:text="Get" />
    33. <Button
    34. android:id="@+id/btn_clear"
    35. android:layout_width="match_parent"
    36. android:layout_height="wrap_content"
    37. android:layout_marginTop="5dp"
    38. android:text="Clear" />
    39. LinearLayout>

    这个文件就是界面里有3个按钮,读,写,清除,然后一个TextView 显示内容,一个EditTexty 用于输入数据。

    三,代码

    首先我们需要定义一个datastore 变量,然后定义2个要操作的key:

    1. //define dataStore
    2. private val Context.dataStore: DataStore by preferencesDataStore(name = "Study")
    3. //定义要操作的key
    4. private val key = stringPreferencesKey("name")
    5. private val keynum = intPreferencesKey("number")

    然后定义2个变量对应textview 和EditText,存取函数put, get, get2

    1. private lateinit var tv: TextView
    2. private lateinit var et:EditText
    3. private suspend fun put() = dataStore.edit { it[key] = "Home"
    4. var num:Int
    5. try {
    6. num = et.text.toString().toInt()
    7. }
    8. catch (ex: Exception)
    9. {
    10. num=2
    11. }
    12. it[keynum]=num
    13. }
    14. private fun get() = runBlocking {
    15. return@runBlocking dataStore.data.map { it[key] ?: "Office" }.first()
    16. }
    17. private fun get2() = runBlocking {
    18. return@runBlocking dataStore.data.map { it[keynum] ?: 5 }.first()
    19. }

    然后在OnCreate 里

    把TextView 和 EditText 对应好。

    3个按钮的对应的按钮事件,以及里面的函数,代码如下:

    1. et=findViewById(R.id.number)
    2. tv=findViewById(R.id.textView)
    3. val btn_put: Button = findViewById(R.id.btn_put)
    4. btn_put.setOnClickListener {
    5. runBlocking { put() }
    6. }
    7. val btn_get: Button = findViewById(R.id.btn_get)
    8. btn_get.setOnClickListener {
    9. val data=get()
    10. val num=get2()
    11. tv.setText("$data count=$num")
    12. }
    13. val btn_clear: Button = findViewById(R.id.btn_clear)
    14. btn_clear.setOnClickListener {
    15. runBlocking { dataStore.edit { it.clear() } }
    16. }

    代码都好了,可以开始运行测试了。

    完整的代码工程在https://github.com/liwenz/datastoreDemo2

    app运行的效果是:初始的时候,textview 显示 hello world

    在clear 或者第一次Get 时,显示 office count=2

    Save 时把home 存name中,编辑行数字存 count 中,

    Get 时把 name 和count 显示在Textview 区, 比如 home count=6677

    本文参考 ​​​​​​Android Jetpack组件 DataStore的使用和简单封装_android jetpack datesouce_初学者-Study的博客-CSDN博客 

    参考的博文内容比较全面,封装了参数设置存取类,同时介绍了 Proto DataStore

    而我的博文力求简单,容易上手。

  • 相关阅读:
    【无标题】
    gitlab 合并分支
    二叉树进程
    文章解读与仿真程序复现思路——电网技术EI\CSCD\北大核心《适应分布式资源渗透率提高的配电网网元规划方法》
    架构整洁之道(一)
    YOLO系列改进
    渗透测试——通过SQL注入拿到webshell
    MySQL数据库
    基础会计学模拟卷
    上网行为审计软件能审计到什么
  • 原文地址:https://blog.csdn.net/leon_zeng0/article/details/133156441