参考资料
【Harmony OS】【JAVA UI】abilitySlice和ability跳转方式
代码实现
1、准备工作
新建一个TwoAbility如下图所示
2、新建PaySlice界面
新建xml文件,在resources的base的layout新建ability_pay.xml文件,代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#ed6262"
ohos:orientation="vertical">
<Text
ohos:height="match_parent"
ohos:width="match_parent"
ohos:text="支付界面"
ohos:text_size="40vp"
ohos:text_color="black"
ohos:text_alignment="center"/>
</DirectionalLayout>
在slice文件目录下新建一个payslice.class文件,代码如下
package com.newdemo.myapplication.slice;
import com.newdemo.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class PaySlice extends AbilitySlice {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_ability_pay);
}
}
3、config.json配置
在config.json文件找到twoability的添加“action.pay”的action,代码如下
"skills": [
{
"actions": [
"action.pay"
]
}
],
4、xml绘画
在ability_main.xml布局添加两个text组件,第一个组件text的内容为“跳转TwoAbility”,另外一个Text组件text的内容为“以Action跳转TwoAbility的payslice”,代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_StartTwoAbility"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="跳转TwoAbility"
ohos:text_size="20vp"
/>
<Text
ohos:id="$+id:text_StartPayWithAction"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="#ed6262"
ohos:layout_alignment="horizontal_center"
ohos:text="以Action跳转TwoAbility的payslice"
ohos:text_size="20vp"
/>
</DirectionalLayout>
mainAbilitySlice代码实现
分别实现跳转TwoAbility和以Action的调到TwoAbility的PaySlice界面,代码如下
package com.newdemo.myapplication.slice;
import com.newdemo.myapplication.ResourceTable;
import com.newdemo.myapplication.TwoAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//todo 跳转到TwoAbility
findComponentById(ResourceTable.Id_text_StartTwoAbility).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent=new Intent();
Operation operation=new Intent.OperationBuilder()
//todo 设置BundleName
.withBundleName(getBundleName())
//todo 设置AbilityName
.withAbilityName(TwoAbility.class.getName())
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
//todo 以Action跳转到TwoAbility的pay界面
findComponentById(ResourceTable.Id_text_StartPayWithAction).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent=new Intent();
Operation operation=new Intent.OperationBuilder()
//todo 设置BundleName
.withBundleName(getBundleName())
//todo 设置AbilityName
.withAbilityName(TwoAbility.class.getName())
//todo 设置Action
.withAction("action.pay")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
}
}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
标签:JAVA,ability,import,TwoAbility,Intent,跳转,intent From: https://www.cnblogs.com/developer-huawei/p/16590691.html