使用DevEco Studio開發鴻蒙系統app

本文檔適用於HarmonyOS應用開發的初學者。編寫兩個簡單的頁面,實現在第一個頁面點擊按鈕跳轉到第二個頁面。

編寫第一個頁面

在Java UI框架中,提供了兩種編寫佈局的方式:在XML中聲明UI佈局和在代碼中創建佈局。這兩種方式創建出的佈局沒有本質差別,為了熟悉兩種方式,我們將通過XML的方式編寫第一個頁面,通過代碼的方式編寫第二個頁面。

XML編寫頁面

  1. 在“Project”窗口,打開“entry > src > main > resources > base”,右鍵點擊“base”文件夾,選擇“New > Directory”,命名為“layout”。


使用DevEco Studio開發鴻蒙系統app

  1. 右鍵點擊“layout”文件夾,選擇“New > File”,命名為“main_layout.xml”。
使用DevEco Studio開發鴻蒙系統app

在“layout”文件夾下可以看到新增了“main_layout.xml”文件。

使用DevEco Studio開發鴻蒙系統app

  1. 打開“main_layout.xml”文件,添加一個文本和一個按鈕,示例代碼如下:
<code>

    
    
/<code>

4.上述按鈕的背景是通過“button_element”來顯示的,需要在“base”目錄下創建“graphic”文件夾,在“graphic”文件夾中新建一個“button_element.xml”文件。

使用DevEco Studio開發鴻蒙系統app

“button_element.xml”的示例代碼如下:

<code>

    
/<code>

加載XML佈局

  1. 在“Project”窗口中,選擇“entry > src > main > java > com.example.helloworld > slice” ,打開“MainAbilitySlice.java”文件。
  2. 重寫onStart()方法加載XML佈局,示例代碼如下:
<code>package com.example.myapplication.slice;
 
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
 
public class MainAbilitySlice extends AbilitySlice {
 
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_layout); // 加載XML佈局
    }
 
    @Override
    public void onActive() {
        super.onActive();
    }
 
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}/<code>


請參考應用運行,效果如圖所示:

使用DevEco Studio開發鴻蒙系統app

創建另一個頁面

創建Feature Ability

  1. 在“Project”窗口,打開“entry > src > main > java”,右鍵點擊“com.example.myapplication”文件夾,選擇“New > Ability > Empty Feature Ability(Java)”。
  2. 配置Ability時,將“Page Name”設置為“SecondAbility”,點擊“Finish”。創建完成後,可以看到新增了“SecondAbility”和“SecondAbilitySlice”文件。
使用DevEco Studio開發鴻蒙系統app

代碼編寫界面

在上一節中,我們用XML的方式編寫了一個包含文本和按鈕的頁面。為了幫助開發者熟悉在代碼中創建佈局的方式,接下來我們使用此方式編寫第二個頁面。

打開 “SecondAbilitySlice.java”文件,添加一個文本,示例代碼如下:

<code>package com.example.myapplication.slice;
 
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DependentLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
 
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
 
public class SecondAbilitySlice extends AbilitySlice {
 
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // 聲明佈局
        DependentLayout myLayout = new DependentLayout(this);
        // 設置佈局大小
        myLayout.setWidth(MATCH_PARENT);
        myLayout.setHeight(MATCH_PARENT);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(0, 0, 0));
        myLayout.setBackground(element);
 
        // 創建一個文本
        Text text = new Text(this);
        text.setText("Nice to meet you.");
        text.setWidth(MATCH_PARENT);
        text.setTextSize(55);
        text.setTextColor(Color.WHITE);
        // 設置文本的佈局
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);
        textConfig.addRule(LayoutConfig.CENTER_IN_PARENT);
        text.setLayoutConfig(textConfig);
        myLayout.addComponent(text);
 
        super.setUIContent(myLayout);
    }
 
    @Override
    public void onActive() {
        super.onActive();
    }
 
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}/<code>

實現頁面跳轉

  1. 打開第一個頁面的“MainAbilitySlice.java”文件,重寫onStart()方法添加按鈕的響應邏輯,實現點擊按鈕跳轉到下一頁,示例代碼如下:
<code>package com.example.myapplication.slice;
 
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
 
public class MainAbilitySlice extends AbilitySlice {
 
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_layout);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
 
        if (button != null) {
            // 為按鈕設置點擊回調
            button.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                Intent secondIntent = new Intent();
                // 指定待啟動FA的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.example.myapplication")
                        .withAbilityName("com.example.myapplication.SecondAbility")
                        .build();
                secondIntent.setOperation(operation);
                startAbility(secondIntent); // 通過AbilitySlice的startAbility接口實現啟動另一個頁面
                }
            });
        }
    }
 
    @Override
    public void onActive() {
        super.onActive();
    }
 
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}/<code>

2.再次運行項目,效果如圖所示:

使用DevEco Studio開發鴻蒙系統app


分享到:


相關文章: