三华为鸿蒙HarmonyOS应用开发HUAWEI DevEco Studio实现页面跳转

Posted 皓月盈江

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三华为鸿蒙HarmonyOS应用开发HUAWEI DevEco Studio实现页面跳转相关的知识,希望对你有一定的参考价值。

在上一节二、华为鸿蒙开发DevEco Studio运行第一个Hello Word工程
基础上进行下面步骤。

在Java UI框架中,提供了两种编写布局的方式:在XML中声明UI布局和在代码中创建布局。这两种方式创建出的布局没有本质差别,为了熟悉两种方式,我们将通过XML的方式编写第一个页面,通过代码的方式编写第二个页面。

一、编写第一个页面

1.在“Project”窗口,点击“entry > src > main > resources > base > layout”,打开“ability_main.xml”文件。
在这里插入图片描述
2.第一个页面内有一个文本和一个按钮,使用DirectionalLayout布局,通过Text和Button组件来实现,其中vp和fp分别表示虚拟像素和字体像素。“ability_main.xml”的示例代码如下:

<?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:alignment="center"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="40vp"
        />

    <Button
        ohos:id="$+id:button"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:text="下一页"
        ohos:text_size="19fp"
        ohos:text_color="#FFFFFF"
        ohos:top_padding="8vp"
        ohos:bottom_padding="8vp"
        ohos:right_padding="70vp"
        ohos:left_padding="70vp"
        ohos:center_in_parent="true"
        ohos:below="$id:text_helloworld"
        ohos:margin="10vp"
        />

</DirectionalLayout>

3.按钮的背景是蓝色胶囊样式,可以通过graphic目录下的XML文件来设置。
右键点击“graphic”文件夹,选择“New > File”,命名为“background_button.xml”,单击回车键。
在这里插入图片描述
“background_button.xml”的示例代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">
    <corners
        ohos:radius="100"/>
    <solid
        ohos:color="#007DFF"/>
</shape>

在layout目录下的“ability_main.xml”文件中,使用background_element="$graphic:background_button"的方式引用“background_button.xml”文件:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    ...
    <Button
        ohos:id="$+id:button"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:text="下一页"
        ohos:text_size="19fp"
        ohos:text_color="#FFFFFF"
        ohos:top_padding="8vp"
        ohos:bottom_padding="8vp"
        ohos:right_padding="70vp"
        ohos:left_padding="70vp"
        ohos:center_in_parent="true"
        ohos:below="$id:text_helloworld"
        ohos:margin="10vp"
        ohos:background_element="$graphic:background_button"
        />
</DirectionalLayout>

4.在XML文件中添加组件后,需要在Java代码中加载XML布局。
在“Project”窗口,选择“entry > src > main > java > com.example.myapplication > slice” ,打开“MainAbilitySlice.java”文件,使用setUIContent方法加载“ability_main.xml”布局。此外,运行代码前需采用import(可使用Alt+Enter快捷键)引入对应类,否则会产生报错提示。

说明
HarmonyOS提供了Ability和AbilitySlice两个基础类,一个有界面的Ability可以由一个或多个AbilitySlice构成,AbilitySlice主要用于承载单个页面的具体逻辑实现和界面UI,是应用显示、运行和跳转的最小单元。

本文档以同一个Ability内的两个AbilitySlice之间的跳转为例,

“MainAbilitySlice.java”的示例代码如下:

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_ability_main);// 加载XML布局
    }

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

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

5.使用预览器或模拟器运行项目,效果如下图所示。
在这里插入图片描述
二、创建另一个页面

上面我们用XML的方式编写了一个包含文本和按钮的页面。为了帮助开发者熟悉在代码中创建布局的方式,接下来我们使用代码的方式编写第二个页面。

1.在“Project”窗口,打开“entry > src > main > java > com.example.myapplication”,右键点击“slice”文件夹,选择“New > Java Class”,命名为“SecondAbilitySlice”,单击回车键。
2.第二个页面上有一个文本。在上一步创建的“SecondAbilitySlice”文件中,添加一个Text,示例代码如下:

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.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.components.DependentLayout.LayoutConfig;

public class SecondAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        // 声明布局
        DependentLayout myLayout = new DependentLayout(this);

        // 设置布局宽高
        myLayout.setWidth(LayoutConfig.MATCH_PARENT);
        myLayout.setHeight(LayoutConfig.MATCH_PARENT);

        // 设置布局背景为白色
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(background);

        // 创建一个文本
        Text text = new Text(this);
        text.setText("华为鸿蒙2.0");
        text.setWidth(LayoutConfig.MATCH_PARENT);
        text.setTextSize(100);
        text.setTextColor(Color.BLACK);

        // 设置文本的布局
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        textConfig.addRule(LayoutConfig.CENTER_IN_PARENT);
        text.setLayoutConfig(textConfig);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }
}

三、实现页面跳转

1.打开第一个页面的“MainAbilitySlice.java”文件,添加按钮的响应逻辑,实现点击按钮跳转到下一页,示例代码如下:

package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);// 加载XML布局

        Button button = (Button) findComponentById(ResourceTable.Id_button);

        // 点击按钮跳转至第二个页面
        button.setClickedListener(listener -> present(new SecondAbilitySlice(), new Intent()));
    }

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

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

2.再次运行项目,效果如下图所示。
在这里插入图片描述

欢迎关注公众号,方便学习交流
在这里插入图片描述

以上是关于三华为鸿蒙HarmonyOS应用开发HUAWEI DevEco Studio实现页面跳转的主要内容,如果未能解决你的问题,请参考以下文章

HarmonyOS应用开发——使用HUAWEI DevEco Studio创建第一个程序 HELLO WORLD!

鸿蒙HarmonyOS应用开发初体验

碰一碰,鸿蒙原子化服务实战

《HarmonyOS实战——前端开发华为鸿蒙系统应用 OpenHarmony JS》

《HarmonyOS实战——前端开发华为鸿蒙系统应用 OpenHarmony JS》

HarmonyOS 2 鸿蒙正式发布