鸿蒙OS应用开发之——页面间跳转

一 概述

本文将创建两个页面,实现页面间跳转

  • 第一个页面MainAbility,第二个页面SecondAbility
  • 每个页面都有一个Text(显示页面内容)和Button(跳转按钮)
  • 在MainAbility点击Next按钮,跳转到SecondAbility
  • SecondAbility点击Return按钮,返回MainAbility

二 项目结构

三 编写第一个页面

3.1 页面组成

  • entry > src > main > resources > base > layout——>ability_main.xml
  • entry > src > main > java > com.example.harmonyosdemo> slice>MainAbilitySlice.java
  • entry > src > main > java > com.example.harmonyosdemo>MainAbility.java

3.2 页面内容

ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#ffffff">

    <Text
        ohos:id="$+id:text"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:center_in_parent="true"
        ohos:text="Page One"
        ohos:text_color="#000000"
        ohos:text_size="32fp"/>

    <Button
        ohos:id="$+id:button"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:align_parent_bottom="true"
        ohos:background_element="$graphic:background_button"
        ohos:bottom_margin="40vp"
        ohos:bottom_padding="8vp"
        ohos:center_in_parent="true"
        ohos:left_padding="70vp"
        ohos:right_padding="70vp"
        ohos:text="Next"
        ohos:text_color="#ffffff"
        ohos:text_size="19fp"
        ohos:top_padding="8vp"
        />
</DependentLayout>
background_button
<?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>
MainAbilitySlice.java
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        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.harmonyosdemo")
                            .withAbilityName("com.example.harmonyosdemo.SecondAbility")
                            .build();
                    secondIntent.setOperation(operation);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(secondIntent);
                }
            });
        }
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
MainAbility
public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
    }
}

四 编写第2个页面

4.1 页面组成

  • entry > src > main > resources > base > layout——>ability_second.xml
  • entry > src > main > java > com.example.harmonyosdemo> slice>SecondAbilitySlice.java
  • entry > src > main > java > com.example.harmonyosdemo>SecondAbility.java

4.2 页面内容

ability_second.xml
<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#ffffff">

    <Text
        ohos:id="$+id:text"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:center_in_parent="true"
        ohos:text="Second Page "
        ohos:text_color="#000000"
        ohos:text_size="32fp"/>

    <Button
        ohos:id="$+id:buttonReturn"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:align_parent_bottom="true"
        ohos:background_element="$graphic:background_button"
        ohos:bottom_margin="40vp"
        ohos:bottom_padding="8vp"
        ohos:center_in_parent="true"
        ohos:left_padding="70vp"
        ohos:right_padding="70vp"
        ohos:text="Return"
        ohos:text_color="#ffffff"
        ohos:text_size="19fp"
        ohos:top_padding="8vp"
        />

</DependentLayout>
SecondAbilitySlice
public class SecondAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // 声明布局
        super.setUIContent(ResourceTable.Layout_ability_second);
        ((Button)findComponentById(ResourceTable.Id_buttonReturn)).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });

//        DependentLayout myLayout = new DependentLayout(this);
//        // 设置布局大小
//        myLayout.setWidth(MATCH_PARENT);
//        myLayout.setHeight(MATCH_PARENT);
//        // 设置布局背景为白色
//        ShapeElement element = new ShapeElement();
//        element.setRgbColor(new RgbColor(255, 255, 255));
//        myLayout.setBackground(element);
//
//        // 创建一个文本
//        Text text = new Text(this);
//        text.setText("Hi there");
//        text.setWidth(MATCH_PARENT);
//        text.setTextSize(100);
//        text.setTextColor(Color.BLACK);
//        // 设置文本的布局
//        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(MATCH_CONTENT,MATCH_CONTENT);
//        textConfig.addRule(DependentLayout.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);
    }
}
SecondAbility
public class SecondAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(SecondAbilitySlice.class.getName());
    }
}

五 效果图

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值