鸿蒙应用开发入门(六):页面间跳转 原创 精华

钟洪发老师
发布于 2020-12-28 13:37
浏览
12收藏

6.1 页面间跳转
1. 认识Intent
Intent是对象之间传递信息的载体。
例如,当一个Ability需要启动另一个Ability时,或者一个AbilitySlice需要导航到另一个AbilitySlice时,可以通过Intent指定启动的目标同时携带相关数据。Intent的构成元素包括Operation与Parameters:

鸿蒙应用开发入门(六):页面间跳转-鸿蒙开发者社区2. 了解AbilitySlice路由配置
虽然一个Page可以包含多个AbilitySlice,但是Page进入前台时界面默认只展示一个AbilitySlice。默认展示的AbilitySlice是通过setMainRoute()方法来指定的。当有多个需要展示的AbilitySlice,可以通过addActionRoute()方法为MainAbilitySlice以外的AbilitySlice配置路由规则。此时,当其他Page实例期望导航到这些AbilitySlice时,可以通过AbilitySlice之间的跳转,显示出这张页面。

public class MyAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // set the main route
        setMainRoute(MainSlice.class.getName());
 
        // set the action route
         addActionRoute("action.pay", PaySlice.class.getName());
        addActionRoute("action.scan", ScanSlice.class.getName());
    }
}

addActionRoute()方法中使用的动作命名,需要在应用配置文件(config.json)中注册:

{
    "module": {
        "abilities": [
            {
                "skills":[
                    {
                        "actions":[
                            "action.pay",
                            "action.scan"
                        ]
                    }
                ]
                ...
            }
        ]
        ...
    }
    ...
}

3. 同一个Page里的AbilitySlice1与AbilitySlice2间的跳转(无参,带参,回值)
1)无参数跳转

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

    Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
    text.setClickedListener(component->{
        Intent intent1 = new Intent();
        present(new MainAbilitySlice1(),intent1);
    });
}

 

2)带参数跳转
(1)产生参数端的AbilitySlice

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

    Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
    text.setClickedListener(component->{
        //有参数跳转
        Intent intent1 = new Intent();
        intent1.setParam("user","钟发发");
        present(new MainAbilitySlice1(),intent1);
    });
}

 

(2)接收参数端的AbilitySlice

public class MainAbilitySlice1 extends AbilitySlice {
    Text text;
    String oldText;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main1);
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld1);
        if(intent != null){
            String user = intent.getStringParam("user");
            oldText = text.getText();
            text.append("," + user);
        }
    }
    @Override
    protected void onInactive() {
        super.onInactive();
    }
....
}

3)带参数跳转+返回值
(1)参数产生端

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

        Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(component->{
            //有参数跳转
            Intent intent1 = new Intent();
            intent1.setParam("user","钟发发");
            presentForResult(new MainAbilitySlice1(),intent1,120);
        });
    }
...
}

(2)参数接收端

public class MainAbilitySlice1 extends AbilitySlice {
    Text text;
    String oldText;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main1);
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld1);
        if(intent != null){
            String user = intent.getStringParam("user");
            oldText = text.getText();
            text.setText(oldText + "," + user);
        }
        //参数接收端在对文字点击
        text.setClickedListener(component -> {
            //1.给跳转来的页面返回值
            Intent intent1 = new Intent();
            intent1.setParam("password","123456");
            setResult(intent1);
            //2.接收本AbilityAlice,自动返回上一页
            terminate();
        });
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        text.setText(oldText);
    }
...
}

(3)回到参数产生端接收返回值

public class MainAbilitySlice extends AbilitySlice {
    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","钟发发");
            presentForResult(new MainAbilitySlice1(),intent1,120);
        });
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if(requestCode==120){
            String password = resultIntent.getStringParam("password");
            text.setText("返回值:" + password);
        }
    }
...
}

4. 不同的Page直接跳转,第一个鸿蒙应用例子写的就是这个,核心代码:

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.myapplication")
                    .withAbilityName("com.example.myapplication.SecondAbility")
                    .build();
            secondIntent.setOperation(operation);
            startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
        }
    });
}

 

5. Page1的MainAbilitySlice跳转Page2的AbilitySlice1

@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.setAction("abilityslice1");  //关键是配置文件里配置action和Ability里注册路由
        startAbility(intent1);
    });
}

 

文章内容已录制成视频课程《鸿蒙手机应用开发入门》https://edu.51cto.com/course/26133.html

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2020-12-28 13:38:23修改
12
收藏 12
回复
举报
7条回复
按时间正序
/
按时间倒序
bugouhen
bugouhen

在老师这里追了一个系列👍

1
回复
2020-12-28 14:00:03
BLUESKYHOST
BLUESKYHOST

返回上一级路由有吗

 

回复
2021-3-19 10:14:07
麒麟Berlin
麒麟Berlin

请问entry中页面跳转到library中页面没反应您知道原因吗?

 

回复
2021-4-7 22:12:33
唐十
唐十

请问,想要设置一个按钮组件在文本组件的下方,通过编程的方法怎么实现这个布局

已于2021-9-8 00:07:22修改
回复
2021-9-7 14:40:38
钟洪发老师
钟洪发老师 回复了 唐十
请问,想要设置一个按钮组件在文本组件的下方,通过编程的方法怎么实现这个布局
DependentLayout dependentLayout = new DependentLayout(this);
Text text1 = new Text(this);
text1.setText("Text1");
text1.setId(0x0001);

Text text2 = new Text(this);
text2.setText("Text2");
dependentLayout.addComponent(text1);
DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig();
layoutConfig.addRule(DependentLayout.LayoutConfig.BELOW,0x0001);
dependentLayout.addComponent(text2,layoutConfig);
回复
2021-9-9 20:46:33
wx61e01b4b282e4
wx61e01b4b282e4

请问老师怎么实现页面2秒后自动跳转?

回复
2022-2-21 00:35:52
钟洪发老师
钟洪发老师

不用看这个了,java的技术路线,鸿蒙已经不用了,转ArkTS哈,虽然可惜,但是java的使用始终是一个雷,华为不得不改!我后续对ArkTS语言的鸿蒙开发也会出一套完整的系列课程,希望关注!

回复
2024-1-21 16:56:35
回复
    相关推荐