首页 > 编程语言 >【JAVA UI】abilitySlice或ability以Action方式跳转

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

时间:2022-08-16 10:29:09浏览次数:86  
标签:JAVA ability import TwoAbility Intent 跳转 intent

​参考资料

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文件,代码如下

<?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"
            ]
          }
        ],

cke_5791.png

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);
            }
        });
    }

}

运行效果

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

欲了解更多更全技术文章,欢迎访问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

相关文章

  • 【Java面试】聊聊你知道的设计模式
    “聊聊你知道的设计模式!”这个问题很简单,但是要让面试官认可你的回答,那还是得花点心思。大家好,我是Mic,一个工作了14年的Java程序员。今天给大家分享一下,当遇到这种比较......
  • windows查看java进程情况
    1.场景有时候我们启动了多个java程序,需要查看各个java程序占用的的内存情况。打开任务管理器选择“进程”,发现有好多java.exe进程,无法直接查看内存使用情况。 jconsol......
  • Day3(复习:java流程控制)
    Java流程控制 Scanner对象用来获取用户的输入基础语法:Scanners=newScanner(System.in) 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取器要......
  • JavaWeb阶段性项目2:QQZone项目梳理
    前置知识前置准备知识准备已掌握JavaSE/MySQL/JDBC+HTML/CSS/JavaScript基础并已完成了Javaweb前置知识的学习01-JavaWeb-HTML初识02-JavaWeb-CSS初识03-JavaWeb-......
  • 8.java NIO
    1.简介2.NIO和BIO的比较1.BIO以流的方式处理数据,而NIO以块的放还是处理数据,块的I/O都效率比流的I/O高很多2.BIO是阻塞的,NIO是非阻塞的3.BIO基于字节流或者字符流进行......
  • JavaWeb4
    1.会话技术会话:一次会话中包含多次请求和响应一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止功能:在一次会话的范围内的多次请求间共享数据......
  • Java-封装结果集示例
    1@Data2publicclassResultDataimplementsSerializable{34/**5*状态码6*/7privateStringcode;89/**10*提示信息11......
  • 代码审计(Java)——WebGoat_Xss
    一、写在前面刚刚入职,适应了几天后抓紧开始学习,毕竟学无止境且自己太菜了……面试的时候,负责人问了我一些关于Java代审的问题,不过之前接触的更多是php的代审。熟......
  • Java的HexUtils
    自己写的一个HexUtils,记录下好Copy。1.pom相关 <alg-bc.version>1.71</alg-bc.version><dependency><groupId>org.bouncycastle</groupId>......
  • [Javascript] Object.freeze: using Object.freeze in function params to enforce im
    functionlotteryNum(){return(Math.round(Math.random()*100)%58)+1;}functionrecordNumber(luckLotteryNumbers:readonlynumber[],num:number):numb......