天天看點

【Java UI】 HarmonyOs如何內建Hawk

作用

Hawk資料存儲工具,使用超簡單,可以替代 ​​Preferences​​,作為本地存儲。Hawk是一個非常便捷的資料庫。 操作資料庫隻需一行代碼 , 能存任何資料類型

參考資料

​​hawk​​

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

項目配置

項目級别bulid.gradle 添加如下代碼

'https://s01.oss.sonatype.org/content/repositories/snapshots/'      
【Java UI】 HarmonyOs如何內建Hawk
【Java UI】 HarmonyOs如何內建Hawk

應用級bulid.gradle添加如下配置(代碼和效果圖如下)

'com.gitee.chinasoft_ohos:hawk:0.0.3-SNAPSHOT'      
【Java UI】 HarmonyOs如何內建Hawk
【Java UI】 HarmonyOs如何內建Hawk

api使用

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

void onInitialize() {
        super.onInitialize();
        Hawk.init(this)
                .build();
    }      
【Java UI】 HarmonyOs如何內建Hawk

設定key value代碼如下

"key","value");      
【Java UI】 HarmonyOs如何內建Hawk

讀取key和value代碼如下

String result=    Hawk.get("key","default");      
【Java UI】 HarmonyOs如何內建Hawk

運作效果

繪畫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>      
【Java UI】 HarmonyOs如何內建Hawk
【Java UI】 HarmonyOs如何內建Hawk

分别實作設定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      
【Java UI】 HarmonyOs如何內建Hawk

運作效果如下

繼續閱讀