【鸿蒙开发】鸿蒙页面之间的跳转

新建鸿蒙项目

这里按照正常创建

创建第二个页面

因为新建一个项目之后就会自动创建一个hello word 的页面,所以我们只需要创建第二个页面,名字自己随便按照取名规范取
在这里插入图片描述

修改第一个页面,并添加跳转事件,跳转到第二个页面

打开MainAbilitySlice文件
按住ctrl点击这个就能打开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:width="match_content"
      ohos:height="match_content"
      ohos:text_size="100"
      ohos:padding="20"
      ohos:text="第一个页面"/>

   <Button
       ohos:id="$+id:but"
       ohos:top_margin="50"
       ohos:width="match_content"
       ohos:height="match_content"
       ohos:padding="20px"
       ohos:layout_alignment="center"
       ohos:text="这是一个按钮"
       ohos:element_left="$media:icon"
       ohos:text_color="#0000FF"
       ohos:text_size="100"
       ohos:background_element="$graphic:but_background"/>

</DirectionalLayout>

然后开始写按钮的点击事件在这里插入图片描述

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

        but1 = (Button) findComponentById(ResourceTable.Id_but);

        but1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("com.example.myapplication")
                        .withAbilityName(PageAbility.class)
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }
        });

    }

然后为了醒目一点,在第二个页面加一个Text

<?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_main1"
        ohos:layout_alignment="horizontal_center"
        ohos:text="第二个页面"
        ohos:text_size="40vp"/>

    <Button
        ohos:id="$+id:but2"
        ohos:top_margin="50"
        ohos:width="match_content"
        ohos:height="match_content"
        ohos:text_size="100"
        ohos:element_left="$media:icon"
        ohos:text="这是第二个按钮"
        ohos:background_element="$graphic:but_background"/>

</DirectionalLayout>

然后运行,点击按钮,就可以跳转到第二个页面了
最后在从第二个页面点击跳转回第一个页面
在这里插入图片描述

public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main1);

        button = (Button) findComponentById(ResourceTable.Id_but2);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("com.example.myapplication")
                        .withAbilityName(MainAbility.class)
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }
        });
    }

这里说明一下

 button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")//这个是跳转设备的ID,如果不传,或者传递一个空的字符串,不是跳转设备
                        .withBundleName("com.example.myapplication")//这个是跳转
                        .withAbilityName(MainAbility.class)
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值