【鸿蒙征程】五.实现页面的跳转✨有参,无参,返回值✨

前言

页面的跳转。在同一个Page里的AbilitySlice1与AbilitySlice2间的跳转(无参,带参,回值)

当一个Ability需要跳转到另一个Ability时,可以通过Intent指定启动的目标,并且携带相关数据,
一个Page可以包含多个AbilitySlice,但是进入前台时只默认展示一个AbilitySlice。并且默认通过setMainRoute()方法来指定,当需要展示多个AbilitySlice时,可以通过addActionRoute()方法为其他的AbilitySlice配置路由。

0.准备工作

建立两个slice:AbilitySlice1和AbilitySlice2
在这里插入图片描述并且继承AbilitySlice,覆盖onStart方法

public class AbilitySlice1 extends AbilitySlice {
    // 继承,覆盖onstart
    protected void onStart(Intent intent){
        super.onStart(intent);
        //指定页面
        super.setUIContent(ResourceTable.Layout_ability_1);

        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld_ability1);
        

然后添加资源Id,创建两个文件在layout里面
在这里插入图片描述
设置资源id

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

第一个为text_helloworld_ability1,第二个可以改为text_helloworld_ability2
然后将两个AbilitySlice和MainAbility关联起来
在这里插入图片描述
其中的action字符串在config.json中设置,并且在这是我们自己自定义的,随后注册到config.json中
在这里插入图片描述

1.无参数跳转

MainAbilitySlice文件中代码实现,通过present()方法来实现跳转

public class MainAbilitySlice extends AbilitySlice {
    private Text text;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 点击事件的监听
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(Component -> {
            Intent intent1 = new Intent();
            present(new AbilitySlice1(),intent1);
   
        });

    }
}

2.带参数跳转

MainAbilitySlice的代码

private Text text;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 点击事件的监听
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(Component -> {
            Intent intent1 = new Intent();
            // 设置传key和值
            intent1.setParam("user","yiming");

            present(new AbilitySlice1(),intent1);
        });

    }

AbilitySlice1的代码

public class AbilitySlice1 extends AbilitySlice {
    // 继承,覆盖onstart
    protected void onStart(Intent intent){
        super.onStart(intent);
        //指定页面
        super.setUIContent(ResourceTable.Layout_ability_1);

        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld_ability1);
        // intent不为空时
        if (intent!=null){
        //  获取值
            String user = intent.getStringParam("user");
            text.append(","+user);
        }
    }
}

3.有返回值的跳转

这里需要用到presentForResult()这个方法来传一个requestcode

MainAbilitySlice的代码

private Text text;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 点击事件的监听
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(Component -> {
            Intent intent1 = new Intent();
            //
            intent1.setParam("user","yiming");
            
            //带有返回值
            presentForResult(new AbilitySlice1(), intent1,1234);
        });

    }
    // 用来接受并且处理返回值的方法。
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode == 1234){
            String pwd = resultIntent.getStringParam("pwd");
            text.append(",密码值:"+pwd);
        }
    }

我们还要用onResult方法接受并处理返回值的方法,并且requestCode指定了需要跳转的页面

AbilitySlice1代码

package com.harmony.hms1.slice;

import com.harmony.hms1.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

public class AbilitySlice1 extends AbilitySlice {
    // 继承,覆盖onstart
    protected void onStart(Intent intent){
        super.onStart(intent);
        //指定页面
        super.setUIContent(ResourceTable.Layout_ability_1);

        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld_ability1);
        if (intent!=null){
            String user = intent.getStringParam("user");
            text.append(","+user);
        }
        // 接受文本,点击事件
        text.setClickedListener(component -> {
            // 返回值接受指令
            Intent intent1 = new Intent();
            intent1.setParam("pwd","12");

            setResult(intent1);
            // 关闭页面,返回上一页
            terminate();
        });
    }
}

如果需要完整的源码,加qq:2024810652,丰富学习资源可找我领取,一起学习一起进步。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弈鸣coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值