当前位置:   article > 正文

【JAVA UI】【HarmonyOS】Preferences的基本使用_java preferences如何使用

java preferences如何使用

 场景介绍:
轻量级数据存储功能通常用于保存应用的一些常用配置信息,并不适合需要存储大量数据和频繁改变数据的场景。应用的数据保存在文件中,这些文件可以持久化地存储在设备上。需要注意的是,应用访问的实例包含文件所有数据,这些数据会一直加载在设备的内存中,直到应用主动从内存中将其移除前,应用可以通过Preferences的API进行数据操作。
接口说明:
轻量级存储为应用提供key-value键值型的文件数据处理能力,支持应用对数据进行轻量级存储及查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括整型、字符串型、布尔型、浮点型、长整型、字符串型Set集合。
参考网址:
文档中心
代码实现:
MainAbilitySlice.java

  1. package com.example.preferencesdemo.slice;
  2. import com.example.preferencesdemo.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Component;
  6. import ohos.agp.components.Text;
  7. import ohos.app.Context;
  8. import ohos.data.DatabaseHelper;
  9. import ohos.data.preferences.Preferences;
  10. public class MainAbilitySlice extends AbilitySlice {
  11. Text textResult;
  12. @Override
  13. public void onStart(Intent intent) {
  14. super.onStart(intent);
  15. super.setUIContent(ResourceTable.Layout_ability_main);
  16. Context context = getContext(); // 数据文件存储路径:/data/data/{PackageName}/{AbilityName}/preferences。
  17. // Context context = getApplicationContext(); // 数据文件存储路径:/data/data/{PackageName}/preferences。
  18. DatabaseHelper databaseHelper = new DatabaseHelper(context); // context入参类型为ohos.app.Context。
  19. String fileName = "test_pref"; // fileName表示文件名,其取值不能为空,也不能包含路径,默认存储目录可以通过context.getPreferencesDir()获取。
  20. Preferences preferences = databaseHelper.getPreferences(fileName);
  21. textResult = findComponentById(ResourceTable.Id_text_result);
  22. //todo 实现点击设置key_value点击事件
  23. findComponentById(ResourceTable.Id_text_putdata).setClickedListener(new Component.ClickedListener() {
  24. @Override
  25. public void onClick(Component component) {
  26. preferences.putInt("intKey", 3);
  27. preferences.putString("StringKey", "String value");
  28. textResult.setText("保存成功");
  29. }
  30. });
  31. //todo 实现读取值得事件
  32. findComponentById(ResourceTable.Id_text_get_data).setClickedListener(new Component.ClickedListener() {
  33. @Override
  34. public void onClick(Component component) {
  35. int value = preferences.getInt("intKey", 0);
  36. String str = preferences.getString("StringKey", "Str");
  37. textResult.setText("读取结果1:"+value+"读取结果2:"+str);
  38. textResult.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
  39. // 始终处于自动滚动状态
  40. textResult.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
  41. // 启动跑马灯效果
  42. textResult.startAutoScrolling();
  43. }
  44. });
  45. }
  46. @Override
  47. public void onActive() {
  48. super.onActive();
  49. }
  50. @Override
  51. public void onForeground(Intent intent) {
  52. super.onForeground(intent);
  53. }
  54. }

ability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="vertical_center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:text_putdata"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:background_element="$graphic:background_ability_main"
  13. ohos:layout_alignment="horizontal_center"
  14. ohos:text="存入数据"
  15. ohos:text_size="40vp"
  16. />
  17. <Text
  18. ohos:id="$+id:text_get_data"
  19. ohos:height="match_content"
  20. ohos:width="match_content"
  21. ohos:text_alignment="center"
  22. ohos:background_element="#FF62EDE1"
  23. ohos:layout_alignment="horizontal_center"
  24. ohos:text="读取数据"
  25. ohos:text_size="40vp"
  26. />
  27. <Text
  28. ohos:id="$+id:text_result"
  29. ohos:height="match_content"
  30. ohos:width="match_content"
  31. ohos:background_element="$graphic:background_ability_main"
  32. ohos:layout_alignment="horizontal_center"
  33. ohos:text="读取结果"
  34. ohos:text_size="30vp"
  35. />
  36. </DirectionalLayout>

运行效果:

2c0651e83bb6f633c104dff4be9b5d5a_494x718.gif%40900-0-90-f.gif

 

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/260058
推荐阅读
相关标签
  

闽ICP备14008679号