天天看點

HarmonyOS 應用開發,完美起航-HarmonyOS應用開發-有界面元能力互動

一、介紹

本篇Codelab将實作的内容

介紹:

HarmonyOS是面向全場景多終端的分布式作業系統,使得應用程式的開發打破了智能終端互通的性能和資料壁壘,業務邏輯原子化開發,适配多端。通過一個簡單應用開發,體驗HarmonyOS的元能力排程能力。

有界面元能力A顯式或隐式拉起另外一個有界面元能力,在拉起的有界面元能力上可以顯示讀到的意圖。

被拉起的有界面元能力可以回資料給元能力A,元能力A顯示收到的傳回資訊。

您将建立什麼

本示例使用gradle建構,請通過DevEco Studio導入此項目。

您将會學到什麼

如何建立一個HarmonyOS Demo Project

如何通過顯式意圖(直接指定需要打開的ability對應的類)和隐式意圖(不明确指定啟動哪個Ability,而是設定Action,讓系統來篩選出合适的Ability)實作界面跳轉以及資料傳遞。

二、您需要什麼

1. 硬體要求

作業系統:Windows10 64位

記憶體:8G及以上。

硬碟:100G及以上。

分辨率:1280*800及以上

2. 軟體要求

需手動下載下傳安裝,詳細步驟請參考《DevEco Studio使用指南》2.1.2

JDK:DevEco Studio自動安裝。

Node.js:請手動下載下傳安裝,詳細步驟請參考《DevEco Studio使用指南》2.1.3 下載下傳和安裝Node.js。

HarmonyOS SDK:待DevEco Studio安裝完成後,利用DevEco Studio來加載HarmonyOS SDK。詳細步驟請參考《DevEco Studio使用指南》2.1.6 加載HarmonyOS SDK。

Maven庫依賴包:如需手動拷貝和配置,詳細步驟請參考《DevEco Studio使用指南》2.3 離線方式配置Maven庫。

3. 需要的知識點

Java基礎開發能力。

三、能力接入準備

實作HarmonyOS應用開發,需要完成以下準備工作:

環境準備。

環境搭建。

建立項目

申請調試證書

應用開發

具體操作,請按照《DevEco Studio使用指南》中詳細說明來完成。

提示:需要通過注冊成開發者才能完成內建準備中的操作。

四、代碼片段

1. 更改其資源檔案相關代碼:

HarmonyOS 應用開發,完美起航-HarmonyOS應用開發-有界面元能力互動

顯示拉起另外一個元能力

2. 編寫第一個頁面

步驟一:使用JAVA編寫第一個頁面。包含一個TextView、一個Button。Button用來跳轉至第二個界面。打開第一個頁面的"MainAbilitySlice.java"檔案,重寫onStart()方法添加按鈕的響應邏輯,實作點選按鈕跳轉到下一頁,示例代碼如下:

@Override

public void onStart(Intent intent) {

super.onStart(intent);

// 步驟1 聲明布局

DirectionalLayout directionalLayout = new DirectionalLayout(this);

// 步驟2 設定布局大小

directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);

directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);

// 步驟3 設定布局屬性及ID(ID視需要設定即可)

directionalLayout.setOrientation(Component.VERTICAL);

directionalLayout.setPadding(5, 5, 5, 5);

Text text = new Text(this);

text.setText("Hello,this is page1.");

text.setTextSize(50);

text.setId(100);

// 步驟4.1 為元件添加對應布局的布局屬性

DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.WRAP_CONTENT,

DirectionalLayout.LayoutConfig.WRAP_CONTENT);

layoutConfig.gravity = LayoutAlignment.TOP;

text.setLayoutConfig(layoutConfig);

// 步驟4.2 将Text添加到布局中

directionalLayout.addComponent(text);

// 類似的添加一個Button

Button button = new Button(this);

layoutConfig.topMargin = 20;

button.setLayoutConfig(layoutConfig);

button.setText("Go_to_page2");

button.setTextSize(50);

button.setId(101);

ShapeElement background = new ShapeElement();

background.setRgbColor(new RgbColor(0,125,255));

background.setCornerRadius(25);

button.setBackground(background);

button.setPadding(5, 5, 5, 5);

button.setClickedListener(new Component.ClickedListener() {

@Override

// 在元件中增加對點選事件的監聽

public void onClick(Component Component) {

// 此處添加按鈕被點選需要執行的操作

Intent secondIntent = new Intent();

// 指定待啟動FA的bundleName和abilityName

ElementName element = new ElementName("", "com.example.tvtest23", "com.example.tvtest23.SecondAbility");

secondIntent.setElement(element);

startAbility(secondIntent); // 通過AbilitySlice的startAbility接口實作啟動另一個頁面

}

});

directionalLayout.addComponent(button);

// 步驟5 将布局作為根布局添加到視圖樹中

super.setUIContent(directionalLayout);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

3. 建立第二個頁面

第二個頁面同理第一個頁面。代碼如下

@Override

public void onStart(Intent intent) {

super.onStart(intent);

LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);

myLayout.setLayoutConfig(config);

ShapeElement element = new ShapeElement();

element.setShape(ShapeElement.RECTANGLE);

element.setRgbColor(new RgbColor(255, 255, 255));

myLayout.setBackground(element);

Text text = new Text(this);

text.setText("Nice to meet you");

text.setTextSize(50);

text.setTextColor(Color.BLACK);

myLayout.addComponent(text);

super.setUIContent(myLayout);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

4. 運作子產品

使用隐式拉起、拉起另外一個元能力并攜帶額外資料、隐式拉起另外一個元能力并攜帶額外資料僅需更改Button的監聽函數。參考代碼如下

隐式拉起另外一個元能力

Intent intent = new Intent();

intent.setAction(ConstUtil.ABILITY_C_ACTION);

intent.addEntity(ConstUtil.ABILITY_C_ENTITY);

startAbilityForResult(intent, ConstUtil.ABILITY_C_REQUEST_CODE);

拉起另外一個元能力并攜帶額外資料

Intent intent = new Intent();

ElementName element = new ElementName("", ConstUtil.BUNDLE_NAME, ConstUtil.ABILITY_B_NAME);

intent.setElement(element);

intent.setParam(ConstUtil.ABILITY_A2B_KEY, ConstUtil.ABILITY_A2B_VALUE);

startAbilityForResult(intent, ConstUtil.ABILITY_B_REQUEST_CODE);

隐式拉起另外一個元能力并攜帶額外資料

Intent intent = new Intent();

intent.setAction(ConstUtil.ABILITY_C_ACTION);

intent.addEntity(ConstUtil.ABILITY_C_ENTITY);

intent.setParam(ConstUtil.ABILITY_A2C_KEY, ConstUtil.ABILITY_A2C_VALUE);

startAbilityForResult(intent, ConstUtil.ABILITY_C_REQUEST_CODE);

設定傳回資料

@Override

protected void onActive() {

super.onActive();

Intent intent = new Intent();

intent.setParam(ConstUtil.ABILITY_B2A_KEY, ConstUtil.ABILITY_B2A_VALUE);

setResult(ConstUtil.ABILITY_B_RESULT_CODE, intent);

}

接受并處理傳回資料

@Override

protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {

switch (requestCode) {

case ConstUtil.ABILITY_B_REQUEST_CODE:

if (resultCode == ConstUtil.ABILITY_B_RESULT_CODE) {

String result = resultData.getStringParam(ConstUtil.ABILITY_B2A_KEY);

text.setText(result);

}

break;

case ConstUtil.ABILITY_C_REQUEST_CODE:

if (resultCode == ConstUtil.ABILITY_C_RESULT_CODE) {

String result = resultData.getStringParam(ConstUtil.ABILITY_C2A_KEY);

text.setText(result);

}

break;

default:

break;

}

}

4. 編譯運作該應用

通過hdc連接配接大屏裝置

先檢視智慧屏IP:

大屏設定->"網絡與連接配接"->"網絡"->"有線網絡"

在cmd或者IDE的Terminal輸入指令:

hdc tconn 192.168.3.9:5555

運作hap

HarmonyOS 應用開發,完美起航-HarmonyOS應用開發-有界面元能力互動

五、恭喜你

幹得好,你已經成功完成了HarmonyOS應用開發E2E體驗,學到了:

如何建立一個HarmonyOS Demo Project

如何建構一個Hap并且将其部署到真機

在HarmonyOS上如何使用HarmonyOS的元能力排程能力