鸿蒙跳转页面

两个页面实现跳转

前期准备

点击
第一页
跳转按钮
第二页

布局

  • xml文件

    标签表示展示不同的内容

    文本

    图片

    按钮

  • Java代码

    在这个目录下面新建一个文件

在这里插入图片描述

IDE会帮我们自动在这里添加

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8eHq7pF0-1629567431783)(C:\Users\29966\Desktop\鸿蒙\img\image-20210822002442628.png)]

在config.json添加的内容

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HE8IafZw-1629567431783)(C:\Users\29966\Desktop\鸿蒙\img\image-20210822002540335.png)]

代码实现

第一页的内容

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.Button;
import ohos.agp.components.Component;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Button btn;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 1.找到按钮 id
        btn = (Button) findComponentById(ResourceTable.Id_btn1);

        // 2. 给按钮添加一个点击事件
        /*
            理解方式
            1. 当使用btn这个按钮点击了的话,执行了本类函数的onClick
         */
        btn.setClickedListener(this);
    }

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

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

    @Override
    public void onClick(Component component) {
        if (component == btn) {
            Intent i = new Intent();
            // 包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("") // 跳转到那个设备上,如果传递一个没有内容的字符串,表示跳转本机
                    .withBundleName("com.example.myapplication") // 我要跳转到那个页面,小括号里面写包名
                    .withAbilityName("com.example.myapplication.SecondAbility") // 要跳转的页面
                    .build(); // 上面的三个信息进行打包
            // 把打包之后的operation设置到意图当中
            i.setOperation(operation);
            // 跳转页面
            startAbility(i);

        }
    }
}

ability_main文件的内容

<?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="first_page"
        ohos:text_size="40vp"
        />

    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="red"
        ohos:text_size="40fp"
        ohos:text="点我"
        />

</DirectionalLayout>

第二页的内容

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.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

public class SecondAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Button btn;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // 创建一个布局对象
        DirectionalLayout dl = new DirectionalLayout(this);

        // 创建文本对象
        Text t = new Text(this);
        // 设置内容
        t.setText("这是第二个页面");
        // 设置文字大小
        t.setTextSize(55);
        // 设置文字颜色
        t.setTextColor(Color.BLUE);
        // 把文本对象添加到布局栏中
        dl.addComponent(t);
        // 把布局添加到子见面中
//        super.setUIContent(dl);

        /*
            1. 添加按钮
            2. 查找按钮
            3. 添加点击事件
         */
        Button btn = new Button(this);
        btn.setText("返回");
        btn.setAutoFontSize(true);
        btn.setTextColor(Color.RED);
        btn.setId(12);
        dl.addComponent(btn);
        super.setUIContent(dl);
        btnClick();
    }

    public void btnClick() {
        btn = (Button) findComponentById(12);
        btn.setClickedListener(this);
    }

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

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

    @Override
    public void onClick(Component component) {
        if (component == btn) {
            Intent i = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.example.myapplication") // 我要跳转到那个页面,小括号里面写包名
                    .withAbilityName("com.example.myapplication.MainAbility")
                    .build();
            i.setOperation(operation);
            startAbility(i);
        }
    };
}

效果图

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值