赞
踩
场景介绍:
轻量级数据存储功能通常用于保存应用的一些常用配置信息,并不适合需要存储大量数据和频繁改变数据的场景。应用的数据保存在文件中,这些文件可以持久化地存储在设备上。需要注意的是,应用访问的实例包含文件所有数据,这些数据会一直加载在设备的内存中,直到应用主动从内存中将其移除前,应用可以通过Preferences的API进行数据操作。
接口说明:
轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括整型、字符串型、布尔型、浮点型、长整型、字符串型Set集合。
参考网址:
文档中心
代码实现:
MainAbilitySlice.java
- package com.example.preferencesdemo.slice;
-
- import com.example.preferencesdemo.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Component;
- import ohos.agp.components.Text;
- import ohos.app.Context;
- import ohos.data.DatabaseHelper;
- import ohos.data.preferences.Preferences;
-
- public class MainAbilitySlice extends AbilitySlice {
- Text textResult;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
-
- Context context = getContext(); // 数据文件存储路径:/data/data/{PackageName}/{AbilityName}/preferences。
- // Context context = getApplicationContext(); // 数据文件存储路径:/data/data/{PackageName}/preferences。
- DatabaseHelper databaseHelper = new DatabaseHelper(context); // context入参类型为ohos.app.Context。
- String fileName = "test_pref"; // fileName表示文件名,其取值不能为空,也不能包含路径,默认存储目录可以通过context.getPreferencesDir()获取。
- Preferences preferences = databaseHelper.getPreferences(fileName);
-
- textResult = findComponentById(ResourceTable.Id_text_result);
- //todo 实现点击设置key_value点击事件
- findComponentById(ResourceTable.Id_text_putdata).setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- preferences.putInt("intKey", 3);
- preferences.putString("StringKey", "String value");
- textResult.setText("保存成功");
- }
- });
- //todo 实现读取值得事件
- findComponentById(ResourceTable.Id_text_get_data).setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- int value = preferences.getInt("intKey", 0);
- String str = preferences.getString("StringKey", "Str");
- textResult.setText("读取结果1:"+value+"读取结果2:"+str);
- textResult.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
- // 始终处于自动滚动状态
- textResult.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
- // 启动跑马灯效果
- textResult.startAutoScrolling();
-
- }
- });
-
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- }

ability_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="vertical_center"
- ohos:orientation="vertical">
-
- <Text
- ohos:id="$+id:text_putdata"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="$graphic:background_ability_main"
- ohos:layout_alignment="horizontal_center"
- ohos:text="存入数据"
- ohos:text_size="40vp"
- />
- <Text
- ohos:id="$+id:text_get_data"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text_alignment="center"
- ohos:background_element="#FF62EDE1"
- ohos:layout_alignment="horizontal_center"
- ohos:text="读取数据"
- ohos:text_size="40vp"
- />
-
- <Text
- ohos:id="$+id:text_result"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="$graphic:background_ability_main"
- ohos:layout_alignment="horizontal_center"
- ohos:text="读取结果"
- ohos:text_size="30vp"
- />
-
- </DirectionalLayout>

运行效果:
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。