1、首先写ability_main.xml代码:里面写三个按钮,分别配置一些提示文字
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="无参无返回的跳转"
ohos:text_size="40vp"
/>
<Button
ohos:id="$+id:but2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参无返回的跳转"
ohos:text_size="40vp"
ohos:top_margin="30vp"
/>
<Button
ohos:id="$+id:but3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参有返回的跳转"
ohos:text_size="40vp"
ohos:top_margin="30vp"
/>
DirectionalLayout>
2、新建三个子界面的xml
abilityslice1.xml的代码:其他两个代码类似,只需修改id和text内容
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="无参无返回的跳转"
ohos:text_size="40vp"
/>
DirectionalLayout>
3、在slice包中仿照MainAbilitySlice.java新建三个java类
MyAbilitySlice1.java代码:其他两个只需修改ResourceTable.Layout_abilityslice1
即可
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class MyAbilitySlice1 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice1);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
4、修改MainAbility.java
package com.example.myapplication;
import com.example.myapplication.slice.MainAbilitySlice;
import com.example.myapplication.slice.MyAbilitySlice1;
import com.example.myapplication.slice.MyAbilitySlice2;
import com.example.myapplication.slice.MyAbilitySlice3;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
//添加主要的子界面
super.setMainRoute(MainAbilitySlice.class.getName());
//添加其他的子界面
super.addActionRoute("slice1", MyAbilitySlice1.class.getName());
super.addActionRoute("slice2", MyAbilitySlice2.class.getName());
super.addActionRoute("slice3", MyAbilitySlice3.class.getName());
}
}
这里面addActionRoute的第一个参数需要在config.json中添加
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home",
"slice1",
"slice2",
"slice3"
]
}
],
"orientation": "unspecified",
"name": "com.example.myapplication.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:entry_MainAbility",
"type": "page",
"launchType": "standard"
}
]
5、修改MainAbilitySlice.java
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button but1;
Button but2;
Button but3;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//找到三个按钮
but1 = (Button) findComponentById(ResourceTable.Id_but1);
but2 = (Button) findComponentById(ResourceTable.Id_but2);
but3 = (Button) findComponentById(ResourceTable.Id_but3);
//给三个按钮绑定事件
but1.setClickedListener(this);
but2.setClickedListener(this);
but3.setClickedListener(this);
}
@Override
public void onClick(Component component) {
if(component == but1){
//无参无返回的跳转
//意图(在跳转的时候想要做的事情)
//一、确定要跳转到哪个设备中的ability中。(operation)
//二、在跳转的时候是否需要带上参数。(parameter)
//如果我们是在同一个ability中跳转,第一个参数可以不写
//如果跳转的时候需要带上参数,就写第二个参数,不需要则可不写
//创建意图的对象
Intent i = new Intent();
//跳转
present(new MyAbilitySlice1(),i);
}else if (component == but2){
//有参无返回的跳转
//创建意图的对象
Intent i = new Intent();
i.setParam("username","张三");
i.setParam("password","123456");
//跳转
present(new MyAbilitySlice2(),i);
}else if(component == but3){
//有参有返回的跳转
//创建意图的对象
Intent i = new Intent();
i.setParam("username","张三");
i.setParam("password","123456");
//跳转
presentForResult(new MyAbilitySlice3(),i,200);
}
}
//参数一:表示请求码
//参数二:返回的意图对象
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if(requestCode == 200){
boolean result = resultIntent.getBooleanParam("result", false);
//吐司
ToastDialog td = new ToastDialog(this);
td.setText(result + "");
td.show();
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
同时修改MyAbilitySlice2.java
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MyAbilitySlice2 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice2);
//方法的参数intent,就是跳转过来的时候传递的意图
String username = intent.getStringParam("username");
String password = intent.getStringParam("password");
Text text = (Text) findComponentById(ResourceTable.Id_text2);
text.setText(username + "," + password);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
修改MyAbilitySlice3.java
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MyAbilitySlice3 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice3);
Text text = (Text) findComponentById(ResourceTable.Id_text3);
//获取到跳转时传递的用户名
String username = intent.getStringParam("username");
text.setText(username);
//当点击了文本框时,返回数据
text.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//创建意图对象,返回结果
Intent i = new Intent();
i.setParam("result",true);
//返回意图
setResult(i);
//关闭当前子界面
terminate();
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}