• 【Java UI】HarmonyOs如何集成Hawk


     作用

    Hawk数据存储工具,使用超简单,可以替代 Preferences,作为本地存储。Hawk是一个非常便捷的数据库。 操作数据库只需一行代码 , 能存任何数据类型

    参考资料

    hawk

    https://www.jianshu.com/p/ee0c35c81c8a

    项目配置

    项目级别bulid.gradle 添加如下代码

    1. maven {
    2. url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
    3. }

    cke_1039.png

    应用级bulid.gradle添加如下配置(代码和效果图如下)

       implementation 'com.gitee.chinasoft_ohos:hawk:0.0.3-SNAPSHOT'

    cke_2015.png

    api使用

    在MyApplication的onInitialize的方法中添加如下代码

    1. @Override
    2. public void onInitialize() {
    3. super.onInitialize();
    4. Hawk.init(this)
    5. .build();
    6. }

    设置key value代码如下

      Hawk.put("key","value");

    读取key和value代码如下

       String result=    Hawk.get("key","default");

    运行效果

    绘画xml 界面,该xml界面存在“设置key_value”和“读取值”和”显示结果“三个text的按钮,代码和运行效果如下

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

    cke_10069.png

    分别实现设置key_value的点击事件和读取值得点击事件,代码如下

    1. package com.harmony.alliance.myapplication.slice;
    2. import com.harmony.alliance.myapplication.ResourceTable;
    3. import com.orhanobut.hawk.Hawk;
    4. import ohos.aafwk.ability.AbilitySlice;
    5. import ohos.aafwk.content.Intent;
    6. import ohos.agp.components.Component;
    7. import ohos.agp.components.Text;
    8. public class MainAbilitySlice extends AbilitySlice {
    9. Text textResult;
    10. @Override
    11. public void onStart(Intent intent) {
    12. super.onStart(intent);
    13. super.setUIContent(ResourceTable.Layout_ability_main);
    14. textResult=findComponentById(ResourceTable.Id_text_result);
    15. //todo 实现点击设置key_value点击事件
    16. findComponentById(ResourceTable.Id_text_putdata).setClickedListener(new Component.ClickedListener() {
    17. @Override
    18. public void onClick(Component component) {
    19. Hawk.put("key","value");
    20. textResult.setText("设置成功");
    21. }
    22. });
    23. //todo 实现读取值得事件
    24. findComponentById(ResourceTable.Id_text_get_data).setClickedListener(new Component.ClickedListener() {
    25. @Override
    26. public void onClick(Component component) {
    27. String result= Hawk.get("key","default");
    28. textResult.setText("读取数据为:"+result);
    29. }
    30. });
    31. }
    32. @Override
    33. public void onActive() {
    34. super.onActive();
    35. }
    36. @Override
    37. public void onForeground(Intent intent) {
    38. super.onForeground(intent);
    39. }
    40. }

    运行效果如下
    34c2dfc6756763823b95441432c62a10_544x960.gif%40900-0-90-f.gif

     

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

  • 相关阅读:
    Cpolar在Linux系统中的应用(网页篇1)
    《垃圾回收算法手册 自动内存管理的艺术》——内存分配(笔记)
    图的创建(邻接表,邻接矩阵)(浙大数据结构代码)
    [力扣] 剑指 Offer 第二天 - 从尾到头打印链表
    自定义View:多点触控(二)-- 多指协作
    基于Vue+SpringBoot的校园电商物流云平台开源项目
    文举论金:黄金原油全面走势分析策略独家指导
    canvas绘制刮涂层抽奖效果
    编译器优化丨Cache优化
    Spring Cloud面试题及答案(2022最新版)
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126067120