【Java UI】 HarmonyOs如何集成Hawk

 作用

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

参考资料

hawk

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

项目配置

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

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

cke_1039.png

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

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

cke_2015.png

api使用

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

  @Override
    public void onInitialize() {
        super.onInitialize();
        Hawk.init(this)
                .build();
    }

设置key value代码如下

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

读取key和value代码如下

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

运行效果

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

<?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: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="设置key_value"
        ohos:text_size="40vp"
        />
    <Text
        ohos:id="$+id:text_get_data"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_alignment="center"
        ohos:background_element="#ed6262"
        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="40vp"
        />

</DirectionalLayout>

cke_10069.png

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

package com.harmony.alliance.myapplication.slice;

import com.harmony.alliance.myapplication.ResourceTable;
import com.orhanobut.hawk.Hawk;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    Text textResult;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        textResult=findComponentById(ResourceTable.Id_text_result);
        //todo 实现点击设置key_value点击事件
        findComponentById(ResourceTable.Id_text_putdata).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Hawk.put("key","value");
                textResult.setText("设置成功");
            }
        });
        //todo 实现读取值得事件
        findComponentById(ResourceTable.Id_text_get_data).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
            String result=    Hawk.get("key","default");
            textResult.setText("读取数据为:"+result);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

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

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

posted @ 2022-08-05 09:41  华为开发者论坛  阅读(38)  评论(0编辑  收藏  举报