赞
踩
原标题:鸿蒙系统中如何实现页面间跳转?
01
认识 Intent
Intent 是对象之间传递信息的载体。
例如,当一个 Ability 需要启动另一个 Ability 时,或者一个 AbilitySlice 需要导航到另一个 AbilitySlice 时,可以通过 Intent 指定启动的目标同时携带相关数据。
Intent 的构成元素包括 Operation 与 Parameters:
02
了解 AbilitySlice 路由配置
虽然一个 Page 可以包含多个 AbilitySlice,但是 Page 进入前台时界面默认只展示一个 AbilitySlice。
默认展示的 AbilitySlice 是通过 setMainRoute 方法来指定的。
当有多个需要展示的 AbilitySlice,可以通过 addActionRoute 方法为 MainAbilitySlice 以外的 AbilitySlice 配置路由规则。
此时,当其他 Page 实例期望导航到这些 AbilitySlice 时,可以通过 AbilitySlice 之间的跳转,显示出这张页面。
publicclassMyAbilityextendsAbility{
@Override
publicvoidonStart(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"
]
}
]
...
}
]
...
}
...
}
03
同一个 Page 里跳转
同一个 Page 里的 AbilitySlice1 与 AbilitySlice2 间的跳转(无参,带参,回值)。
①无参数跳转
@Override
publicvoidonStart(Intent intent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(component->{
Intent intent1 = newIntent;
present( newMainAbilitySlice1,intent1);
});
}
②带参数跳转
产生参数端的 AbilitySlice:
@Override
publicvoidonStart(Intent intent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(component->{
//有参数跳转
Intent intent1 = newIntent;
intent1.setParam( "user", "钟发发");
present( newMainAbilitySlice1,intent1);
});
}
接收参数端的 AbilitySlice:
publicclassMainAbilitySlice1extendsAbilitySlice{
Text text;
String oldText;
@Override
publicvoidonStart(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
protectedvoidonInactive{
super.onInactive;
}
....
}
③带参数跳转+返回值
参数产生端:
publicclassMainAbilitySliceextendsAbilitySlice{
@Override
publicvoidonStart(Intent intent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(component->{
//有参数跳转
Intent intent1 = newIntent;
intent1.setParam( "user", "钟发发");
presentForResult( newMainAbilitySlice1,intent1, 120);
});
}
...
}
参数接收端:
publicclassMainAbilitySlice1extendsAbilitySlice{
Text text;
String oldText;
@Override
publicvoidonStart(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 = newIntent;
intent1.setParam( "password", "123456");
setResult(intent1);
//2.接收本AbilityAlice,自动返回上一页
terminate;
});
}
@Override
protectedvoidonInactive{
super.onInactive;
text.setText(oldText);
}
...
}
回到参数产生端接收返回值:
publicclassMainAbilitySliceextendsAbilitySlice{
Text text;
@Override
publicvoidonStart(Intent intent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(component->{
//有参数跳转
Intent intent1 = newIntent;
intent1.setParam( "user", "钟发发");
presentForResult( newMainAbilitySlice1,intent1, 120);
});
}
@Override
protectedvoidonResult( intrequestCode, Intent resultIntent){
super.onResult(requestCode, resultIntent);
if(requestCode== 120){
String password = resultIntent.getStringParam( "password");
text.setText( "返回值:"+ password);
}
}
...
}
04
不同的 Page 直接跳转
第一个鸿蒙应用例子写的就是这个,核心代码:
if(button != null) {
// 为按钮设置点击回调
button.setClickedListener( newComponent.ClickedListener {
@Override
publicvoidonClick(Component component){
Intent secondIntent = newIntent;
// 指定待启动FA的bundleName和abilityName
Operation operation = newIntent.OperationBuilder
.withDeviceId( "")
.withBundleName( "com.example.myapplication")
.withAbilityName( "com.example.myapplication.SecondAbility")
.build;
secondIntent.setOperation(operation);
startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
}
});
}
Page1 的 MainAbilitySlice 跳转 Page2 的 AbilitySlice1:
@Override
publicvoidonStart(Intent intent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(component->{
Intent intent1 = newIntent;
intent1.setAction( "abilityslice1"); //关键是配置文件里配置action和Ability里注册路由
startAbility(intent1);
});
}
文章内容已录制成视频课程《鸿蒙手机应用开发入门》:
https: //edu.51cto.com/course/26133.html
责任编辑:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。