• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3734

entry/06-May-2025-1,2861,116

hvigor/06-May-2025-2322

screenshots/devices/06-May-2025-

.gitignoreD06-May-2025112 88

README.mdD06-May-20251,022 3015

README_zh.mdD06-May-20252.1 KiB7349

build-profile.json5D06-May-20251 KiB4241

hvigorfile.tsD06-May-2025158 21

hvigorwD06-May-20252.1 KiB6228

hvigorw.batD06-May-20252 KiB7256

oh-package.json5D06-May-2025815 2625

ohosTest.mdD06-May-20251.2 KiB1714

README.md

1#  Preferences
2
3### Introduction
4
5This sample shows how to alternate between themes using preferences. The display effect is as follows:
6
7![](screenshots/devices/default_en.png)
8
9### Concepts
10
11Preferences: provide capabilities for processing data in the form of key-value (KV) pairs and support lightweight data persistence, modification, and query. In KV pairs, the keys are of the string type, and the values can be of the number, string, Boolean, Array\<number>, Array\<string>, or Array\<boolean> type.
12
13### Required Permissions
14
15N/A.
16
17### Usage
18
191. Touch the switch button on the upper right corner of the title bar, and select the target theme from the screen displayed.
20
212. Exit the app and then access it again. The theme before the last exit is displayed.
22
23### Constraints
24
25- This sample can only be run on standard-system devices.
26
27- This sample is based on the stage model, which is supported from API version 9.
28
29- This sample requires DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100) to compile and run.
30

README_zh.md

1#  首选项
2
3### 简介
4
5本示例使用[@ohos.data.preferences](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkdata/js-apis-data-preferences.md)
6接口,展示了使用首选项持久化存储数据的功能。
7
8### 效果预览
9
10|default主题|pomelo主题|simplicity主题|
11|---|---|---|
12|![](screenshots/devices/default.png)|![](screenshots/devices/pomelo.png)|![](screenshots/devices/simplicity.png)|
13
14使用说明
15
161.点击顶部titleBar的右侧**切换**按钮,弹出主题菜单,选择任意主题则切换相应的主题界面;
17
182.退出应用再重新进入,显示上一次退出前的主题界面。
19
20### 工程目录
21```
22entry/src/main/ets/
23|---Application
24|---common
25|   |---ThemeDesktop.ets      // 首页主体内容
26|---MainAbility
27|---filemanager
28|   |---data
29|---model                     // 日志文件
30|---pages
31|   |---Index.ets             // 首页,所有的接口和数据都封装在这里
32|---util
33|   |---DataSource.ets        // 数据懒加载
34```
35
36### 具体实现
37
38* 切换主题:在首页预先设置好几套主体数据,使用preferences.getPreferences获取使用Preferences对象,调用Preferences.get()
39读取缓存中的参数,得到当前应该展示哪一套主体。每次点击切换按钮都会调用Preferences.put()来重新修改参数,然后使用
40Preferences.flush()保存并刷新文件内容。
41源码参考:[Index.ets](entry/src/main/ets/pages/Index.ets) 。
42
43### 相关权限
44
45不涉及。
46
47### 依赖
48
49不涉及。
50
51### 约束与限制
52
531. 本示例仅支持在标准系统上运行。
54
552. 本示例为Stage模型,从API version 9开始支持。
56
573. 本示例已适配API version 9版本SDK,版本号:3.2.11.9。
58
594. 本示例需要使用DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400 构建 2023年4月7日)及以上版本才可编译运行。
60
61### 下载
62
63如需单独下载本工程,执行如下命令:
64
65```
66git init
67git config core.sparsecheckout true
68echo code/BasicFeature/DataManagement/Preferences/ > .git/info/sparse-checkout
69git remote add origin https://gitee.com/openharmony/applications_app_samples.git
70git pull origin master
71```
72
73