• 【JAVA UI】abilitySlice或ability以Action方式跳转


     参考资料

    Page Ability基本概念

    【Harmony OS】【JAVA UI】abilitySlice和ability跳转方式

    【HarmonyOS】【FAQ】鸿蒙问题合集4

    代码实现

    1、准备工作

    新建一个TwoAbility如下图所示

    cke_347.png

    2、新建PaySlice界面

    新建xml文件,在resources的base的layout新建ability_pay.xml文件,代码如下

    1. <DirectionalLayout
    2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    3. ohos:height="match_parent"
    4. ohos:width="match_parent"
    5. ohos:background_element="#ed6262"
    6. ohos:orientation="vertical">
    7. <Text
    8. ohos:height="match_parent"
    9. ohos:width="match_parent"
    10. ohos:text="支付界面"
    11. ohos:text_size="40vp"
    12. ohos:text_color="black"
    13. ohos:text_alignment="center"/>
    14. DirectionalLayout>

    在slice文件目录下新建一个payslice.class文件,代码如下

    1. package com.newdemo.myapplication.slice;
    2. import com.newdemo.myapplication.ResourceTable;
    3. import ohos.aafwk.ability.AbilitySlice;
    4. import ohos.aafwk.content.Intent;
    5. public class PaySlice extends AbilitySlice {
    6. @Override
    7. protected void onStart(Intent intent) {
    8. super.onStart(intent);
    9. setUIContent(ResourceTable.Layout_ability_pay);
    10. }
    11. }

    3、config.json配置

    在config.json文件找到twoability的添加“action.pay”的action,代码如下

    1. "skills": [
    2. {
    3. "actions": [
    4. "action.pay"
    5. ]
    6. }
    7. ],

    cke_5791.png

    4、xml绘画

    在ability_main.xml布局添加两个text组件,第一个组件text的内容为“跳转TwoAbility”,另外一个Text组件text的内容为“以Action跳转TwoAbility的payslice”,代码如下

    1. <DirectionalLayout
    2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    3. ohos:height="match_parent"
    4. ohos:width="match_parent"
    5. ohos:alignment="center"
    6. ohos:orientation="vertical">
    7. <Text
    8. ohos:id="$+id:text_StartTwoAbility"
    9. ohos:height="100vp"
    10. ohos:width="match_parent"
    11. ohos:text_alignment="center"
    12. ohos:background_element="$graphic:background_ability_main"
    13. ohos:layout_alignment="horizontal_center"
    14. ohos:text="跳转TwoAbility"
    15. ohos:text_size="20vp"
    16. />
    17. <Text
    18. ohos:id="$+id:text_StartPayWithAction"
    19. ohos:height="100vp"
    20. ohos:width="match_parent"
    21. ohos:text_alignment="center"
    22. ohos:background_element="#ed6262"
    23. ohos:layout_alignment="horizontal_center"
    24. ohos:text="以Action跳转TwoAbility的payslice"
    25. ohos:text_size="20vp"
    26. />
    27. DirectionalLayout>

    mainAbilitySlice代码实现

    分别实现跳转TwoAbility和以Action的调到TwoAbility的PaySlice界面,代码如下

    1. package com.newdemo.myapplication.slice;
    2. import com.newdemo.myapplication.ResourceTable;
    3. import com.newdemo.myapplication.TwoAbility;
    4. import ohos.aafwk.ability.AbilitySlice;
    5. import ohos.aafwk.content.Intent;
    6. import ohos.aafwk.content.Operation;
    7. import ohos.agp.components.Component;
    8. public class MainAbilitySlice extends AbilitySlice {
    9. @Override
    10. public void onStart(Intent intent) {
    11. super.onStart(intent);
    12. super.setUIContent(ResourceTable.Layout_ability_main);
    13. //todo 跳转到TwoAbility
    14. findComponentById(ResourceTable.Id_text_StartTwoAbility).setClickedListener(new Component.ClickedListener() {
    15. @Override
    16. public void onClick(Component component) {
    17. Intent intent=new Intent();
    18. Operation operation=new Intent.OperationBuilder()
    19. //todo 设置BundleName
    20. .withBundleName(getBundleName())
    21. //todo 设置AbilityName
    22. .withAbilityName(TwoAbility.class.getName())
    23. .build();
    24. intent.setOperation(operation);
    25. startAbility(intent);
    26. }
    27. });
    28. //todo 以Action跳转到TwoAbility的pay界面
    29. findComponentById(ResourceTable.Id_text_StartPayWithAction).setClickedListener(new Component.ClickedListener() {
    30. @Override
    31. public void onClick(Component component) {
    32. Intent intent=new Intent();
    33. Operation operation=new Intent.OperationBuilder()
    34. //todo 设置BundleName
    35. .withBundleName(getBundleName())
    36. //todo 设置AbilityName
    37. .withAbilityName(TwoAbility.class.getName())
    38. //todo 设置Action
    39. .withAction("action.pay")
    40. .build();
    41. intent.setOperation(operation);
    42. startAbility(intent);
    43. }
    44. });
    45. }
    46. }

    运行效果

    4bec62a3f5cde9992e8e752dc11a34ae_416x949.gif%40900-0-90-f.gif

     

     欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

  • 相关阅读:
    jsencrypt.js加密java后端解密
    spring-boot-maven-plugin 配置是干啥的?
    PaddlePaddle:开源深度学习平台
    二分类资料校准曲线calibration curve的绘制
    线性回归模型
    ping命令
    基于神经网络的语音识别
    HarmonyOS(29)onMeasureSize和PlaceChildren (View的测量和布局)
    ROS2入门之基本介绍
    前端入门的一些知识
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/126359882