https://harmonyos.51cto.com/#zz
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
©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任
https://harmonyos.51cto.com/#zz
JSP spring boot / cloud 使用filter防止XSS 一.前言 XSS(跨站脚本攻击) 跨站脚...
首先插件配备好了,写一个HTML测试一下 首先创建一个文件夹,创建一个HTML 文件...
1.ajax跨域传递值是所需要的回传的类型为jsonp $.ajax({url: "http://.......",t...
在项目开始之前我们可以先去了解一下IConfiguration接口,.Net Core Web应用程序...
2 月 18 日消息 据外媒 Windowslatest 报道,在预览版本中发现的参考资料表明,...
昨天刚学了html的一些内容,就迫不及待的想做个京东上面的搜索条,结果做是做出...
一.前言 .NET Core 是一个通用开发平台,由 Microsoft 和 GitHub 上的 .NET 社区...
第一课趣味二进制——修改植物大战僵尸数据 任务介绍 学习目标 知识需求 需求工...
文章目录 前言 本周最重要的五件事情 本周搞砸的四件事情 本周的四个启发 前言 ...
IT之家2月18日消息外媒 Windows Latest 报道,微软正在与谷歌合作进行一项新的改...