HarmonyOS APP 开发入门

打印 上一主题 下一主题

主题 546|帖子 546|积分 1638

前言


HarmonyOS官方定义

鸿蒙是一个全场景,面向未来的操作系统。
解释:
鸿蒙并不是一个单纯的手机操作系统,而是手机 + 智能设备的总称。可以安装在包括手机、手表、无人机等很多 设备上。
鸿蒙分为两类:


  • 应用开发:也是整个鸿蒙的控制中心。(手机app)
    开发语言:Java,js,C/C++(华为目前建议使用的还是Java)
  • 内核:基于Linux内核开发的
  • 设备开发:也就是无人机,摄像机等硬件设备。最合适的开发语言:C/C++
一、 安装鸿蒙编译器 --DevEco Studio

1.适合开发鸿蒙的语言


​ Java,C/C++,JavaScript,eTS,cangjie(仓颉)


  • Java,js适合手机应用的开发
  • C/C++适用于硬件开发
    1. 在以后的鸿蒙版本中(华为计划是3.0),即将推出仓颉语言开发。(已经开始内测,还没可以使用)
    复制代码
​ 但是就目前而言,华为主推的还是Java。
2.鸿蒙开发的完整流程



3.Java开发的准备工作


1)注册开发者(略)
2)下载开发工具
DevEco Studio就是鸿蒙应用开发的开发工具,是基于IntelliJ IDEA Community开源版本打造的,所以如果大家有 idea的基础,在学习DevEco Studio是非常容易上手的。
DevEco Studio中提供SDK Manager统一管理SDK和这个包依赖的工具链。通过SDK Manager能自动下载各个语 言对应的SDK包。
4)下载网址:HarmonyOS
5)版本说明
​ DevEco Studio 1.0 用于华为EMUI系统的应用App。

​ DevEco Studio 2.0 用于华为鸿蒙系统(HarmonyOS)的应用App。
​ DevEco Studio 3.0 用于华为鸿蒙系统(openHarmonyOS)的应用App。
4)使用Java开发
JDK:是Java语言所需要的开发环境。鸿蒙开发工具DevEco Studio会帮我们自动下载。
!!!鸿蒙开发工具目前只支持JDK8
5)常用插件

  • Toolchains:SDK工具链,HarmonyOS应用开发必备工具集,包括编译、打包、签名、数据库管理等工具的集 合,首次下载SDK时默认下载。
  • Previewer:Lite Wearable预览器,在开发过程中可以动态预览Lite Wearable应用的界面呈现效果,默认不自 动下载,需手动勾选下载。
6)安装开发工具
​ 傻瓜式安装–略
4.DevEco Studio项目结构(Java)




  • MyApplication:整个项目的项目名
  • External Libraries:项目中要导入的第三方jar包
  • .gradle:是项目管理工具gradle的信息(一般是不会动)
  • .idea:是开发工具的信息(一般不会动)
  • entry:是应用主模块,在一个项目中,有且只有一个,所有的代码都在这个里面。(重要)
  • gradle:项目管理工具gradle的配置信息和所需要用到的jar包,在这个里面有一个porperties的配置文件, 可以看到gradle的版本号,一般我们会用默认的,也不会动
  • build.gradle:第三方jar包的配置文件,以后在开发中要用到第三方jar包需要在这个里面配置。
  • build.gradle:第三方jar包的配置文件,以后在开发中要用到第三方jar包需要在这个里面配置。(一个是linux系统的,一个是windows系统的。)
  • local.properties:本地SDK配置信息,一般也不动。
  • build:一般不会动,这个文件夹里面有一个R文件,记录每一个资源文件对应的id。里面的内容是自动生成的。(R文件是一个压缩包,里面装的ResourceTable这个类的字节码文件。)
  • libs:第三方jar包存放的位置
  • src:主要代码(src里面还有三个目录,main,ohosTest,test)
  • src里面还有三个目录
  • src里面还有三个目录
  • test:是Junit的测试工具。

二、第一个HarmonyOS项目(官方教程解析)


1.第一个Harmony入门应用HelloWorld

​ 对于Helloworld,相信大家都不会陌生,我们学习大部分语言的时候,所做的第一个项目就是HelloWorld。
​ 当我们新建第一个项目的时候,我们的Studio就会自己生成一个HelloWorld项目,这也是我们正式入门HarmonyOS APP移动开发的第一个项目。
​ 对于这个项目,我们需要了解的知识点只有四个
​ 1、如何运行项目
​ 2、页面中的包含关系
​ 3、配置文件:confifig.json
​ 4、了解程序的启动流程
2.运行项目


  • 要运行项目就需要我们去登录我们的华为开发者账号,点击Studio右上角的登录按钮登录即可
  • 要运行项目,我们就需要使用到模拟器来运行项目,华为官方为我们提供了两种模拟器,一种是本地模拟器,一种是在线模拟器,我们将使用在线模拟器来讲解。

点击工具(tools)下的Device Manger,进入如图所示页面

在这里我们选择Remote Emulator,选择需要的手机型号,点击绿色的三角形就行启动模拟器
这里需要注意,我们这里的API与我们所安装的HarmonyOS SDK 是息息相关的,如果你没有安装对应版本的SDK那么程序就会运行错误
3. 页面中的包含关系

​ MainAbility是项目启动的第一个界面,在界面里面不是直接显示内容的。在界面里面展示的是子界面,我们也称之为切片,或者片 段,在子界面里面才是显示内容。

如图所示,MainAbility是我们第一个所展示的页面,MainAbilitySlice就是一个子页面,而我们的内容只能在子页面里展现出来。


  • MainAbility (界面)包含一个或多个MainAbilitySlice(子界面)
  • MainAbilitySlice(子界面)包含要显示的内容
4.配置文件:config.json

​ config.json 是整个APP项目的配置文件。所有项目信息都会配置在这里,如,版本号,厂商信息等

5.程序运行过程


6.实现页面跳转

通过代码实现以下操作

设计步骤:
  1.         1. 设置页面文本内容,按钮组件
  2.         2. 设置第二个页面内容
  3.         3. 设置时间进行跳转
复制代码
下面先来对Layout下的xml文件进行编辑
补充知识:
在MainAbilitySlice.java里

如图,ResourceTable.Layout_ability_main表示的是在resource下有一个叫ability_main.xml的文件,这个文件是我们的主页面,HarmonyOS的app开发是通过xml来进行页面设计的。
下面我们正式开始进行编程解决这个问题
ability_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4.     ohos:height="match_parent"
  5.     ohos:width="match_parent"
  6.     ohos:alignment="center"
  7.     ohos:orientation="vertical">
  8.     <Text
  9.         ohos:id="$+id:text_helloworld"
  10.         ohos:height="match_content"
  11.         ohos:width="match_content"
  12.         ohos:background_element="$graphic:background_ability_main"
  13.         ohos:layout_alignment="horizontal_center"
  14.         ohos:text="itcyy第一个页面"
  15.         ohos:text_size="40vp"
  16.         />
  17.     <Button
  18.         ohos:id="$+id:cyy_one_button"
  19.         ohos:height="match_content"
  20.         ohos:width="match_content"
  21.         ohos:text_size="40fp"
  22.         ohos:background_element="red"
  23.         ohos:text="点我"/>
  24. </DirectionalLayout>
复制代码
下面需要建立一个新的切片

选择slice包,右键

我们选择新建>Ability >Empty Page Ability(Java)

在这里我们就可以新建切片了,不要选择图中的Launcher ability,否则它就会变成我们的主页面了
我们在HarmonyOS开发里,有两种布局方式,一种是xml,一种是直接用java进行编写,在这里,我们第二个页面通过Java编写
cyy__two_AbilitySlice.java
  1. package com.itcyy.myapplication.slice;import com.itcyy.myapplication.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.DirectionalLayout;import ohos.agp.components.Text;import ohos.agp.utils.Color;public class cyy_two_AbilitySlice extends AbilitySlice {    @Override    public void onStart(Intent intent) {<?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4.     ohos:height="match_parent"
  5.     ohos:width="match_parent"
  6.     ohos:alignment="center"
  7.     ohos:orientation="vertical">
  8.     <Text
  9.         ohos:id="$+id:text_helloworld"
  10.         ohos:height="match_content"
  11.         ohos:width="match_content"
  12.         ohos:background_element="$graphic:background_ability_main"
  13.         ohos:layout_alignment="horizontal_center"
  14.         ohos:text="itcyy第一个页面"
  15.         ohos:text_size="40vp"
  16.         />
  17.     <Button
  18.         ohos:id="$+id:cyy_one_button"
  19.         ohos:height="match_content"
  20.         ohos:width="match_content"
  21.         ohos:text_size="40fp"
  22.         ohos:background_element="red"
  23.         ohos:text="点我"/>
  24. </DirectionalLayout>super.onStart(intent);<?xml version="1.0" encoding="utf-8"?>
  25. <DirectionalLayout
  26.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  27.     ohos:height="match_parent"
  28.     ohos:width="match_parent"
  29.     ohos:alignment="center"
  30.     ohos:orientation="vertical">
  31.     <Text
  32.         ohos:id="$+id:text_helloworld"
  33.         ohos:height="match_content"
  34.         ohos:width="match_content"
  35.         ohos:background_element="$graphic:background_ability_main"
  36.         ohos:layout_alignment="horizontal_center"
  37.         ohos:text="itcyy第一个页面"
  38.         ohos:text_size="40vp"
  39.         />
  40.     <Button
  41.         ohos:id="$+id:cyy_one_button"
  42.         ohos:height="match_content"
  43.         ohos:width="match_content"
  44.         ohos:text_size="40fp"
  45.         ohos:background_element="red"
  46.         ohos:text="点我"/>
  47. </DirectionalLayout>//super.setUIContent(ResourceTable.Layout_ability_cyy_two_);<?xml version="1.0" encoding="utf-8"?>
  48. <DirectionalLayout
  49.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  50.     ohos:height="match_parent"
  51.     ohos:width="match_parent"
  52.     ohos:alignment="center"
  53.     ohos:orientation="vertical">
  54.     <Text
  55.         ohos:id="$+id:text_helloworld"
  56.         ohos:height="match_content"
  57.         ohos:width="match_content"
  58.         ohos:background_element="$graphic:background_ability_main"
  59.         ohos:layout_alignment="horizontal_center"
  60.         ohos:text="itcyy第一个页面"
  61.         ohos:text_size="40vp"
  62.         />
  63.     <Button
  64.         ohos:id="$+id:cyy_one_button"
  65.         ohos:height="match_content"
  66.         ohos:width="match_content"
  67.         ohos:text_size="40fp"
  68.         ohos:background_element="red"
  69.         ohos:text="点我"/>
  70. </DirectionalLayout>//1.创建一个布局对象<?xml version="1.0" encoding="utf-8"?>
  71. <DirectionalLayout
  72.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  73.     ohos:height="match_parent"
  74.     ohos:width="match_parent"
  75.     ohos:alignment="center"
  76.     ohos:orientation="vertical">
  77.     <Text
  78.         ohos:id="$+id:text_helloworld"
  79.         ohos:height="match_content"
  80.         ohos:width="match_content"
  81.         ohos:background_element="$graphic:background_ability_main"
  82.         ohos:layout_alignment="horizontal_center"
  83.         ohos:text="itcyy第一个页面"
  84.         ohos:text_size="40vp"
  85.         />
  86.     <Button
  87.         ohos:id="$+id:cyy_one_button"
  88.         ohos:height="match_content"
  89.         ohos:width="match_content"
  90.         ohos:text_size="40fp"
  91.         ohos:background_element="red"
  92.         ohos:text="点我"/>
  93. </DirectionalLayout>DirectionalLayout dl = new DirectionalLayout(this);<?xml version="1.0" encoding="utf-8"?>
  94. <DirectionalLayout
  95.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  96.     ohos:height="match_parent"
  97.     ohos:width="match_parent"
  98.     ohos:alignment="center"
  99.     ohos:orientation="vertical">
  100.     <Text
  101.         ohos:id="$+id:text_helloworld"
  102.         ohos:height="match_content"
  103.         ohos:width="match_content"
  104.         ohos:background_element="$graphic:background_ability_main"
  105.         ohos:layout_alignment="horizontal_center"
  106.         ohos:text="itcyy第一个页面"
  107.         ohos:text_size="40vp"
  108.         />
  109.     <Button
  110.         ohos:id="$+id:cyy_one_button"
  111.         ohos:height="match_content"
  112.         ohos:width="match_content"
  113.         ohos:text_size="40fp"
  114.         ohos:background_element="red"
  115.         ohos:text="点我"/>
  116. </DirectionalLayout>//2.创建一个文本对象<?xml version="1.0" encoding="utf-8"?>
  117. <DirectionalLayout
  118.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  119.     ohos:height="match_parent"
  120.     ohos:width="match_parent"
  121.     ohos:alignment="center"
  122.     ohos:orientation="vertical">
  123.     <Text
  124.         ohos:id="$+id:text_helloworld"
  125.         ohos:height="match_content"
  126.         ohos:width="match_content"
  127.         ohos:background_element="$graphic:background_ability_main"
  128.         ohos:layout_alignment="horizontal_center"
  129.         ohos:text="itcyy第一个页面"
  130.         ohos:text_size="40vp"
  131.         />
  132.     <Button
  133.         ohos:id="$+id:cyy_one_button"
  134.         ohos:height="match_content"
  135.         ohos:width="match_content"
  136.         ohos:text_size="40fp"
  137.         ohos:background_element="red"
  138.         ohos:text="点我"/>
  139. </DirectionalLayout>Text t = new Text(this);<?xml version="1.0" encoding="utf-8"?>
  140. <DirectionalLayout
  141.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  142.     ohos:height="match_parent"
  143.     ohos:width="match_parent"
  144.     ohos:alignment="center"
  145.     ohos:orientation="vertical">
  146.     <Text
  147.         ohos:id="$+id:text_helloworld"
  148.         ohos:height="match_content"
  149.         ohos:width="match_content"
  150.         ohos:background_element="$graphic:background_ability_main"
  151.         ohos:layout_alignment="horizontal_center"
  152.         ohos:text="itcyy第一个页面"
  153.         ohos:text_size="40vp"
  154.         />
  155.     <Button
  156.         ohos:id="$+id:cyy_one_button"
  157.         ohos:height="match_content"
  158.         ohos:width="match_content"
  159.         ohos:text_size="40fp"
  160.         ohos:background_element="red"
  161.         ohos:text="点我"/>
  162. </DirectionalLayout>//设置内容<?xml version="1.0" encoding="utf-8"?>
  163. <DirectionalLayout
  164.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  165.     ohos:height="match_parent"
  166.     ohos:width="match_parent"
  167.     ohos:alignment="center"
  168.     ohos:orientation="vertical">
  169.     <Text
  170.         ohos:id="$+id:text_helloworld"
  171.         ohos:height="match_content"
  172.         ohos:width="match_content"
  173.         ohos:background_element="$graphic:background_ability_main"
  174.         ohos:layout_alignment="horizontal_center"
  175.         ohos:text="itcyy第一个页面"
  176.         ohos:text_size="40vp"
  177.         />
  178.     <Button
  179.         ohos:id="$+id:cyy_one_button"
  180.         ohos:height="match_content"
  181.         ohos:width="match_content"
  182.         ohos:text_size="40fp"
  183.         ohos:background_element="red"
  184.         ohos:text="点我"/>
  185. </DirectionalLayout>t.setText("cyy第二个页面");<?xml version="1.0" encoding="utf-8"?>
  186. <DirectionalLayout
  187.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  188.     ohos:height="match_parent"
  189.     ohos:width="match_parent"
  190.     ohos:alignment="center"
  191.     ohos:orientation="vertical">
  192.     <Text
  193.         ohos:id="$+id:text_helloworld"
  194.         ohos:height="match_content"
  195.         ohos:width="match_content"
  196.         ohos:background_element="$graphic:background_ability_main"
  197.         ohos:layout_alignment="horizontal_center"
  198.         ohos:text="itcyy第一个页面"
  199.         ohos:text_size="40vp"
  200.         />
  201.     <Button
  202.         ohos:id="$+id:cyy_one_button"
  203.         ohos:height="match_content"
  204.         ohos:width="match_content"
  205.         ohos:text_size="40fp"
  206.         ohos:background_element="red"
  207.         ohos:text="点我"/>
  208. </DirectionalLayout>//设置大小<?xml version="1.0" encoding="utf-8"?>
  209. <DirectionalLayout
  210.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  211.     ohos:height="match_parent"
  212.     ohos:width="match_parent"
  213.     ohos:alignment="center"
  214.     ohos:orientation="vertical">
  215.     <Text
  216.         ohos:id="$+id:text_helloworld"
  217.         ohos:height="match_content"
  218.         ohos:width="match_content"
  219.         ohos:background_element="$graphic:background_ability_main"
  220.         ohos:layout_alignment="horizontal_center"
  221.         ohos:text="itcyy第一个页面"
  222.         ohos:text_size="40vp"
  223.         />
  224.     <Button
  225.         ohos:id="$+id:cyy_one_button"
  226.         ohos:height="match_content"
  227.         ohos:width="match_content"
  228.         ohos:text_size="40fp"
  229.         ohos:background_element="red"
  230.         ohos:text="点我"/>
  231. </DirectionalLayout>t.setTextSize(55);<?xml version="1.0" encoding="utf-8"?>
  232. <DirectionalLayout
  233.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  234.     ohos:height="match_parent"
  235.     ohos:width="match_parent"
  236.     ohos:alignment="center"
  237.     ohos:orientation="vertical">
  238.     <Text
  239.         ohos:id="$+id:text_helloworld"
  240.         ohos:height="match_content"
  241.         ohos:width="match_content"
  242.         ohos:background_element="$graphic:background_ability_main"
  243.         ohos:layout_alignment="horizontal_center"
  244.         ohos:text="itcyy第一个页面"
  245.         ohos:text_size="40vp"
  246.         />
  247.     <Button
  248.         ohos:id="$+id:cyy_one_button"
  249.         ohos:height="match_content"
  250.         ohos:width="match_content"
  251.         ohos:text_size="40fp"
  252.         ohos:background_element="red"
  253.         ohos:text="点我"/>
  254. </DirectionalLayout>//设置文本颜色<?xml version="1.0" encoding="utf-8"?>
  255. <DirectionalLayout
  256.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  257.     ohos:height="match_parent"
  258.     ohos:width="match_parent"
  259.     ohos:alignment="center"
  260.     ohos:orientation="vertical">
  261.     <Text
  262.         ohos:id="$+id:text_helloworld"
  263.         ohos:height="match_content"
  264.         ohos:width="match_content"
  265.         ohos:background_element="$graphic:background_ability_main"
  266.         ohos:layout_alignment="horizontal_center"
  267.         ohos:text="itcyy第一个页面"
  268.         ohos:text_size="40vp"
  269.         />
  270.     <Button
  271.         ohos:id="$+id:cyy_one_button"
  272.         ohos:height="match_content"
  273.         ohos:width="match_content"
  274.         ohos:text_size="40fp"
  275.         ohos:background_element="red"
  276.         ohos:text="点我"/>
  277. </DirectionalLayout>t.setTextColor(Color.BLUE);<?xml version="1.0" encoding="utf-8"?>
  278. <DirectionalLayout
  279.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  280.     ohos:height="match_parent"
  281.     ohos:width="match_parent"
  282.     ohos:alignment="center"
  283.     ohos:orientation="vertical">
  284.     <Text
  285.         ohos:id="$+id:text_helloworld"
  286.         ohos:height="match_content"
  287.         ohos:width="match_content"
  288.         ohos:background_element="$graphic:background_ability_main"
  289.         ohos:layout_alignment="horizontal_center"
  290.         ohos:text="itcyy第一个页面"
  291.         ohos:text_size="40vp"
  292.         />
  293.     <Button
  294.         ohos:id="$+id:cyy_one_button"
  295.         ohos:height="match_content"
  296.         ohos:width="match_content"
  297.         ohos:text_size="40fp"
  298.         ohos:background_element="red"
  299.         ohos:text="点我"/>
  300. </DirectionalLayout>//将文本对象加入布局中<?xml version="1.0" encoding="utf-8"?>
  301. <DirectionalLayout
  302.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  303.     ohos:height="match_parent"
  304.     ohos:width="match_parent"
  305.     ohos:alignment="center"
  306.     ohos:orientation="vertical">
  307.     <Text
  308.         ohos:id="$+id:text_helloworld"
  309.         ohos:height="match_content"
  310.         ohos:width="match_content"
  311.         ohos:background_element="$graphic:background_ability_main"
  312.         ohos:layout_alignment="horizontal_center"
  313.         ohos:text="itcyy第一个页面"
  314.         ohos:text_size="40vp"
  315.         />
  316.     <Button
  317.         ohos:id="$+id:cyy_one_button"
  318.         ohos:height="match_content"
  319.         ohos:width="match_content"
  320.         ohos:text_size="40fp"
  321.         ohos:background_element="red"
  322.         ohos:text="点我"/>
  323. </DirectionalLayout>dl.addComponent(t);<?xml version="1.0" encoding="utf-8"?>
  324. <DirectionalLayout
  325.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  326.     ohos:height="match_parent"
  327.     ohos:width="match_parent"
  328.     ohos:alignment="center"
  329.     ohos:orientation="vertical">
  330.     <Text
  331.         ohos:id="$+id:text_helloworld"
  332.         ohos:height="match_content"
  333.         ohos:width="match_content"
  334.         ohos:background_element="$graphic:background_ability_main"
  335.         ohos:layout_alignment="horizontal_center"
  336.         ohos:text="itcyy第一个页面"
  337.         ohos:text_size="40vp"
  338.         />
  339.     <Button
  340.         ohos:id="$+id:cyy_one_button"
  341.         ohos:height="match_content"
  342.         ohos:width="match_content"
  343.         ohos:text_size="40fp"
  344.         ohos:background_element="red"
  345.         ohos:text="点我"/>
  346. </DirectionalLayout>//4.把创建的布局加入到子页面中<?xml version="1.0" encoding="utf-8"?>
  347. <DirectionalLayout
  348.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  349.     ohos:height="match_parent"
  350.     ohos:width="match_parent"
  351.     ohos:alignment="center"
  352.     ohos:orientation="vertical">
  353.     <Text
  354.         ohos:id="$+id:text_helloworld"
  355.         ohos:height="match_content"
  356.         ohos:width="match_content"
  357.         ohos:background_element="$graphic:background_ability_main"
  358.         ohos:layout_alignment="horizontal_center"
  359.         ohos:text="itcyy第一个页面"
  360.         ohos:text_size="40vp"
  361.         />
  362.     <Button
  363.         ohos:id="$+id:cyy_one_button"
  364.         ohos:height="match_content"
  365.         ohos:width="match_content"
  366.         ohos:text_size="40fp"
  367.         ohos:background_element="red"
  368.         ohos:text="点我"/>
  369. </DirectionalLayout>super.setUIContent(dl);    }    @Override    public void onActive() {<?xml version="1.0" encoding="utf-8"?>
  370. <DirectionalLayout
  371.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  372.     ohos:height="match_parent"
  373.     ohos:width="match_parent"
  374.     ohos:alignment="center"
  375.     ohos:orientation="vertical">
  376.     <Text
  377.         ohos:id="$+id:text_helloworld"
  378.         ohos:height="match_content"
  379.         ohos:width="match_content"
  380.         ohos:background_element="$graphic:background_ability_main"
  381.         ohos:layout_alignment="horizontal_center"
  382.         ohos:text="itcyy第一个页面"
  383.         ohos:text_size="40vp"
  384.         />
  385.     <Button
  386.         ohos:id="$+id:cyy_one_button"
  387.         ohos:height="match_content"
  388.         ohos:width="match_content"
  389.         ohos:text_size="40fp"
  390.         ohos:background_element="red"
  391.         ohos:text="点我"/>
  392. </DirectionalLayout>super.onActive();    }    @Override    public void onForeground(Intent intent) {<?xml version="1.0" encoding="utf-8"?>
  393. <DirectionalLayout
  394.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  395.     ohos:height="match_parent"
  396.     ohos:width="match_parent"
  397.     ohos:alignment="center"
  398.     ohos:orientation="vertical">
  399.     <Text
  400.         ohos:id="$+id:text_helloworld"
  401.         ohos:height="match_content"
  402.         ohos:width="match_content"
  403.         ohos:background_element="$graphic:background_ability_main"
  404.         ohos:layout_alignment="horizontal_center"
  405.         ohos:text="itcyy第一个页面"
  406.         ohos:text_size="40vp"
  407.         />
  408.     <Button
  409.         ohos:id="$+id:cyy_one_button"
  410.         ohos:height="match_content"
  411.         ohos:width="match_content"
  412.         ohos:text_size="40fp"
  413.         ohos:background_element="red"
  414.         ohos:text="点我"/>
  415. </DirectionalLayout>super.onForeground(intent);    }}
复制代码
MainAbilitySlice.java
  1. package com.itcyy.myapplication.slice;import com.itcyy.myapplication.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Button;import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {    Button cyy_button;    @Override    public void onStart(Intent intent) {<?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4.     ohos:height="match_parent"
  5.     ohos:width="match_parent"
  6.     ohos:alignment="center"
  7.     ohos:orientation="vertical">
  8.     <Text
  9.         ohos:id="$+id:text_helloworld"
  10.         ohos:height="match_content"
  11.         ohos:width="match_content"
  12.         ohos:background_element="$graphic:background_ability_main"
  13.         ohos:layout_alignment="horizontal_center"
  14.         ohos:text="itcyy第一个页面"
  15.         ohos:text_size="40vp"
  16.         />
  17.     <Button
  18.         ohos:id="$+id:cyy_one_button"
  19.         ohos:height="match_content"
  20.         ohos:width="match_content"
  21.         ohos:text_size="40fp"
  22.         ohos:background_element="red"
  23.         ohos:text="点我"/>
  24. </DirectionalLayout>super.onStart(intent);<?xml version="1.0" encoding="utf-8"?>
  25. <DirectionalLayout
  26.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  27.     ohos:height="match_parent"
  28.     ohos:width="match_parent"
  29.     ohos:alignment="center"
  30.     ohos:orientation="vertical">
  31.     <Text
  32.         ohos:id="$+id:text_helloworld"
  33.         ohos:height="match_content"
  34.         ohos:width="match_content"
  35.         ohos:background_element="$graphic:background_ability_main"
  36.         ohos:layout_alignment="horizontal_center"
  37.         ohos:text="itcyy第一个页面"
  38.         ohos:text_size="40vp"
  39.         />
  40.     <Button
  41.         ohos:id="$+id:cyy_one_button"
  42.         ohos:height="match_content"
  43.         ohos:width="match_content"
  44.         ohos:text_size="40fp"
  45.         ohos:background_element="red"
  46.         ohos:text="点我"/>
  47. </DirectionalLayout>super.setUIContent(ResourceTable.Layout_ability_main);<?xml version="1.0" encoding="utf-8"?>
  48. <DirectionalLayout
  49.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  50.     ohos:height="match_parent"
  51.     ohos:width="match_parent"
  52.     ohos:alignment="center"
  53.     ohos:orientation="vertical">
  54.     <Text
  55.         ohos:id="$+id:text_helloworld"
  56.         ohos:height="match_content"
  57.         ohos:width="match_content"
  58.         ohos:background_element="$graphic:background_ability_main"
  59.         ohos:layout_alignment="horizontal_center"
  60.         ohos:text="itcyy第一个页面"
  61.         ohos:text_size="40vp"
  62.         />
  63.     <Button
  64.         ohos:id="$+id:cyy_one_button"
  65.         ohos:height="match_content"
  66.         ohos:width="match_content"
  67.         ohos:text_size="40fp"
  68.         ohos:background_element="red"
  69.         ohos:text="点我"/>
  70. </DirectionalLayout>//1.通过id寻找创建的按钮<?xml version="1.0" encoding="utf-8"?>
  71. <DirectionalLayout
  72.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  73.     ohos:height="match_parent"
  74.     ohos:width="match_parent"
  75.     ohos:alignment="center"
  76.     ohos:orientation="vertical">
  77.     <Text
  78.         ohos:id="$+id:text_helloworld"
  79.         ohos:height="match_content"
  80.         ohos:width="match_content"
  81.         ohos:background_element="$graphic:background_ability_main"
  82.         ohos:layout_alignment="horizontal_center"
  83.         ohos:text="itcyy第一个页面"
  84.         ohos:text_size="40vp"
  85.         />
  86.     <Button
  87.         ohos:id="$+id:cyy_one_button"
  88.         ohos:height="match_content"
  89.         ohos:width="match_content"
  90.         ohos:text_size="40fp"
  91.         ohos:background_element="red"
  92.         ohos:text="点我"/>
  93. </DirectionalLayout>//Button cyy_button = (Button)findComponentById(ResourceTable.Id_cyy_one_button);<?xml version="1.0" encoding="utf-8"?>
  94. <DirectionalLayout
  95.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  96.     ohos:height="match_parent"
  97.     ohos:width="match_parent"
  98.     ohos:alignment="center"
  99.     ohos:orientation="vertical">
  100.     <Text
  101.         ohos:id="$+id:text_helloworld"
  102.         ohos:height="match_content"
  103.         ohos:width="match_content"
  104.         ohos:background_element="$graphic:background_ability_main"
  105.         ohos:layout_alignment="horizontal_center"
  106.         ohos:text="itcyy第一个页面"
  107.         ohos:text_size="40vp"
  108.         />
  109.     <Button
  110.         ohos:id="$+id:cyy_one_button"
  111.         ohos:height="match_content"
  112.         ohos:width="match_content"
  113.         ohos:text_size="40fp"
  114.         ohos:background_element="red"
  115.         ohos:text="点我"/>
  116. </DirectionalLayout>cyy_button = (Button)findComponentById(ResourceTable.Id_cyy_one_button);<?xml version="1.0" encoding="utf-8"?>
  117. <DirectionalLayout
  118.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  119.     ohos:height="match_parent"
  120.     ohos:width="match_parent"
  121.     ohos:alignment="center"
  122.     ohos:orientation="vertical">
  123.     <Text
  124.         ohos:id="$+id:text_helloworld"
  125.         ohos:height="match_content"
  126.         ohos:width="match_content"
  127.         ohos:background_element="$graphic:background_ability_main"
  128.         ohos:layout_alignment="horizontal_center"
  129.         ohos:text="itcyy第一个页面"
  130.         ohos:text_size="40vp"
  131.         />
  132.     <Button
  133.         ohos:id="$+id:cyy_one_button"
  134.         ohos:height="match_content"
  135.         ohos:width="match_content"
  136.         ohos:text_size="40fp"
  137.         ohos:background_element="red"
  138.         ohos:text="点我"/>
  139. </DirectionalLayout>//2.给按钮添加一个点击事件<?xml version="1.0" encoding="utf-8"?>
  140. <DirectionalLayout
  141.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  142.     ohos:height="match_parent"
  143.     ohos:width="match_parent"
  144.     ohos:alignment="center"
  145.     ohos:orientation="vertical">
  146.     <Text
  147.         ohos:id="$+id:text_helloworld"
  148.         ohos:height="match_content"
  149.         ohos:width="match_content"
  150.         ohos:background_element="$graphic:background_ability_main"
  151.         ohos:layout_alignment="horizontal_center"
  152.         ohos:text="itcyy第一个页面"
  153.         ohos:text_size="40vp"
  154.         />
  155.     <Button
  156.         ohos:id="$+id:cyy_one_button"
  157.         ohos:height="match_content"
  158.         ohos:width="match_content"
  159.         ohos:text_size="40fp"
  160.         ohos:background_element="red"
  161.         ohos:text="点我"/>
  162. </DirectionalLayout>//如果按钮没有添加点击事件,那么点击按钮将不会有任何反应<?xml version="1.0" encoding="utf-8"?>
  163. <DirectionalLayout
  164.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  165.     ohos:height="match_parent"
  166.     ohos:width="match_parent"
  167.     ohos:alignment="center"
  168.     ohos:orientation="vertical">
  169.     <Text
  170.         ohos:id="$+id:text_helloworld"
  171.         ohos:height="match_content"
  172.         ohos:width="match_content"
  173.         ohos:background_element="$graphic:background_ability_main"
  174.         ohos:layout_alignment="horizontal_center"
  175.         ohos:text="itcyy第一个页面"
  176.         ohos:text_size="40vp"
  177.         />
  178.     <Button
  179.         ohos:id="$+id:cyy_one_button"
  180.         ohos:height="match_content"
  181.         ohos:width="match_content"
  182.         ohos:text_size="40fp"
  183.         ohos:background_element="red"
  184.         ohos:text="点我"/>
  185. </DirectionalLayout>//如果我们给按钮添加了点击事件的话,那么我们点击按钮之后就会执行相应的代码<?xml version="1.0" encoding="utf-8"?>
  186. <DirectionalLayout
  187.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  188.     ohos:height="match_parent"
  189.     ohos:width="match_parent"
  190.     ohos:alignment="center"
  191.     ohos:orientation="vertical">
  192.     <Text
  193.         ohos:id="$+id:text_helloworld"
  194.         ohos:height="match_content"
  195.         ohos:width="match_content"
  196.         ohos:background_element="$graphic:background_ability_main"
  197.         ohos:layout_alignment="horizontal_center"
  198.         ohos:text="itcyy第一个页面"
  199.         ohos:text_size="40vp"
  200.         />
  201.     <Button
  202.         ohos:id="$+id:cyy_one_button"
  203.         ohos:height="match_content"
  204.         ohos:width="match_content"
  205.         ohos:text_size="40fp"
  206.         ohos:background_element="red"
  207.         ohos:text="点我"/>
  208. </DirectionalLayout>//当我们点击了cyy_button这个按钮之后,那么就会实现本类的onClick方法<?xml version="1.0" encoding="utf-8"?>
  209. <DirectionalLayout
  210.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  211.     ohos:height="match_parent"
  212.     ohos:width="match_parent"
  213.     ohos:alignment="center"
  214.     ohos:orientation="vertical">
  215.     <Text
  216.         ohos:id="$+id:text_helloworld"
  217.         ohos:height="match_content"
  218.         ohos:width="match_content"
  219.         ohos:background_element="$graphic:background_ability_main"
  220.         ohos:layout_alignment="horizontal_center"
  221.         ohos:text="itcyy第一个页面"
  222.         ohos:text_size="40vp"
  223.         />
  224.     <Button
  225.         ohos:id="$+id:cyy_one_button"
  226.         ohos:height="match_content"
  227.         ohos:width="match_content"
  228.         ohos:text_size="40fp"
  229.         ohos:background_element="red"
  230.         ohos:text="点我"/>
  231. </DirectionalLayout>cyy_button.setClickedListener(this);//注意这里是需要继承Component.ClickedListener接口的    }    @Override    public void onActive() {<?xml version="1.0" encoding="utf-8"?>
  232. <DirectionalLayout
  233.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  234.     ohos:height="match_parent"
  235.     ohos:width="match_parent"
  236.     ohos:alignment="center"
  237.     ohos:orientation="vertical">
  238.     <Text
  239.         ohos:id="$+id:text_helloworld"
  240.         ohos:height="match_content"
  241.         ohos:width="match_content"
  242.         ohos:background_element="$graphic:background_ability_main"
  243.         ohos:layout_alignment="horizontal_center"
  244.         ohos:text="itcyy第一个页面"
  245.         ohos:text_size="40vp"
  246.         />
  247.     <Button
  248.         ohos:id="$+id:cyy_one_button"
  249.         ohos:height="match_content"
  250.         ohos:width="match_content"
  251.         ohos:text_size="40fp"
  252.         ohos:background_element="red"
  253.         ohos:text="点我"/>
  254. </DirectionalLayout>super.onActive();    }    @Override    public void onForeground(Intent intent) {<?xml version="1.0" encoding="utf-8"?>
  255. <DirectionalLayout
  256.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  257.     ohos:height="match_parent"
  258.     ohos:width="match_parent"
  259.     ohos:alignment="center"
  260.     ohos:orientation="vertical">
  261.     <Text
  262.         ohos:id="$+id:text_helloworld"
  263.         ohos:height="match_content"
  264.         ohos:width="match_content"
  265.         ohos:background_element="$graphic:background_ability_main"
  266.         ohos:layout_alignment="horizontal_center"
  267.         ohos:text="itcyy第一个页面"
  268.         ohos:text_size="40vp"
  269.         />
  270.     <Button
  271.         ohos:id="$+id:cyy_one_button"
  272.         ohos:height="match_content"
  273.         ohos:width="match_content"
  274.         ohos:text_size="40fp"
  275.         ohos:background_element="red"
  276.         ohos:text="点我"/>
  277. </DirectionalLayout>super.onForeground(intent);    }    @Override    public void onClick(Component component) {<?xml version="1.0" encoding="utf-8"?>
  278. <DirectionalLayout
  279.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  280.     ohos:height="match_parent"
  281.     ohos:width="match_parent"
  282.     ohos:alignment="center"
  283.     ohos:orientation="vertical">
  284.     <Text
  285.         ohos:id="$+id:text_helloworld"
  286.         ohos:height="match_content"
  287.         ohos:width="match_content"
  288.         ohos:background_element="$graphic:background_ability_main"
  289.         ohos:layout_alignment="horizontal_center"
  290.         ohos:text="itcyy第一个页面"
  291.         ohos:text_size="40vp"
  292.         />
  293.     <Button
  294.         ohos:id="$+id:cyy_one_button"
  295.         ohos:height="match_content"
  296.         ohos:width="match_content"
  297.         ohos:text_size="40fp"
  298.         ohos:background_element="red"
  299.         ohos:text="点我"/>
  300. </DirectionalLayout>//点击按钮需要执行的代码<?xml version="1.0" encoding="utf-8"?>
  301. <DirectionalLayout
  302.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  303.     ohos:height="match_parent"
  304.     ohos:width="match_parent"
  305.     ohos:alignment="center"
  306.     ohos:orientation="vertical">
  307.     <Text
  308.         ohos:id="$+id:text_helloworld"
  309.         ohos:height="match_content"
  310.         ohos:width="match_content"
  311.         ohos:background_element="$graphic:background_ability_main"
  312.         ohos:layout_alignment="horizontal_center"
  313.         ohos:text="itcyy第一个页面"
  314.         ohos:text_size="40vp"
  315.         />
  316.     <Button
  317.         ohos:id="$+id:cyy_one_button"
  318.         ohos:height="match_content"
  319.         ohos:width="match_content"
  320.         ohos:text_size="40fp"
  321.         ohos:background_element="red"
  322.         ohos:text="点我"/>
  323. </DirectionalLayout>//跳转到第二个页面<?xml version="1.0" encoding="utf-8"?>
  324. <DirectionalLayout
  325.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  326.     ohos:height="match_parent"
  327.     ohos:width="match_parent"
  328.     ohos:alignment="center"
  329.     ohos:orientation="vertical">
  330.     <Text
  331.         ohos:id="$+id:text_helloworld"
  332.         ohos:height="match_content"
  333.         ohos:width="match_content"
  334.         ohos:background_element="$graphic:background_ability_main"
  335.         ohos:layout_alignment="horizontal_center"
  336.         ohos:text="itcyy第一个页面"
  337.         ohos:text_size="40vp"
  338.         />
  339.     <Button
  340.         ohos:id="$+id:cyy_one_button"
  341.         ohos:height="match_content"
  342.         ohos:width="match_content"
  343.         ohos:text_size="40fp"
  344.         ohos:background_element="red"
  345.         ohos:text="点我"/>
  346. </DirectionalLayout>if (component  == cyy_button){<?xml version="1.0" encoding="utf-8"?>
  347. <DirectionalLayout
  348.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  349.     ohos:height="match_parent"
  350.     ohos:width="match_parent"
  351.     ohos:alignment="center"
  352.     ohos:orientation="vertical">
  353.     <Text
  354.         ohos:id="$+id:text_helloworld"
  355.         ohos:height="match_content"
  356.         ohos:width="match_content"
  357.         ohos:background_element="$graphic:background_ability_main"
  358.         ohos:layout_alignment="horizontal_center"
  359.         ohos:text="itcyy第一个页面"
  360.         ohos:text_size="40vp"
  361.         />
  362.     <Button
  363.         ohos:id="$+id:cyy_one_button"
  364.         ohos:height="match_content"
  365.         ohos:width="match_content"
  366.         ohos:text_size="40fp"
  367.         ohos:background_element="red"
  368.         ohos:text="点我"/>
  369. </DirectionalLayout>    //只有点击了cyy_button 这个按钮后才进行跳转<?xml version="1.0" encoding="utf-8"?>
  370. <DirectionalLayout
  371.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  372.     ohos:height="match_parent"
  373.     ohos:width="match_parent"
  374.     ohos:alignment="center"
  375.     ohos:orientation="vertical">
  376.     <Text
  377.         ohos:id="$+id:text_helloworld"
  378.         ohos:height="match_content"
  379.         ohos:width="match_content"
  380.         ohos:background_element="$graphic:background_ability_main"
  381.         ohos:layout_alignment="horizontal_center"
  382.         ohos:text="itcyy第一个页面"
  383.         ohos:text_size="40vp"
  384.         />
  385.     <Button
  386.         ohos:id="$+id:cyy_one_button"
  387.         ohos:height="match_content"
  388.         ohos:width="match_content"
  389.         ohos:text_size="40fp"
  390.         ohos:background_element="red"
  391.         ohos:text="点我"/>
  392. </DirectionalLayout>    //跳转到哪个页面(意图)<?xml version="1.0" encoding="utf-8"?>
  393. <DirectionalLayout
  394.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  395.     ohos:height="match_parent"
  396.     ohos:width="match_parent"
  397.     ohos:alignment="center"
  398.     ohos:orientation="vertical">
  399.     <Text
  400.         ohos:id="$+id:text_helloworld"
  401.         ohos:height="match_content"
  402.         ohos:width="match_content"
  403.         ohos:background_element="$graphic:background_ability_main"
  404.         ohos:layout_alignment="horizontal_center"
  405.         ohos:text="itcyy第一个页面"
  406.         ohos:text_size="40vp"
  407.         />
  408.     <Button
  409.         ohos:id="$+id:cyy_one_button"
  410.         ohos:height="match_content"
  411.         ohos:width="match_content"
  412.         ohos:text_size="40fp"
  413.         ohos:background_element="red"
  414.         ohos:text="点我"/>
  415. </DirectionalLayout>    //创建意图对象<?xml version="1.0" encoding="utf-8"?>
  416. <DirectionalLayout
  417.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  418.     ohos:height="match_parent"
  419.     ohos:width="match_parent"
  420.     ohos:alignment="center"
  421.     ohos:orientation="vertical">
  422.     <Text
  423.         ohos:id="$+id:text_helloworld"
  424.         ohos:height="match_content"
  425.         ohos:width="match_content"
  426.         ohos:background_element="$graphic:background_ability_main"
  427.         ohos:layout_alignment="horizontal_center"
  428.         ohos:text="itcyy第一个页面"
  429.         ohos:text_size="40vp"
  430.         />
  431.     <Button
  432.         ohos:id="$+id:cyy_one_button"
  433.         ohos:height="match_content"
  434.         ohos:width="match_content"
  435.         ohos:text_size="40fp"
  436.         ohos:background_element="red"
  437.         ohos:text="点我"/>
  438. </DirectionalLayout>    Intent i = new Intent();<?xml version="1.0" encoding="utf-8"?>
  439. <DirectionalLayout
  440.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  441.     ohos:height="match_parent"
  442.     ohos:width="match_parent"
  443.     ohos:alignment="center"
  444.     ohos:orientation="vertical">
  445.     <Text
  446.         ohos:id="$+id:text_helloworld"
  447.         ohos:height="match_content"
  448.         ohos:width="match_content"
  449.         ohos:background_element="$graphic:background_ability_main"
  450.         ohos:layout_alignment="horizontal_center"
  451.         ohos:text="itcyy第一个页面"
  452.         ohos:text_size="40vp"
  453.         />
  454.     <Button
  455.         ohos:id="$+id:cyy_one_button"
  456.         ohos:height="match_content"
  457.         ohos:width="match_content"
  458.         ohos:text_size="40fp"
  459.         ohos:background_element="red"
  460.         ohos:text="点我"/>
  461. </DirectionalLayout>    //包含了要跳转页面的详细信息<?xml version="1.0" encoding="utf-8"?>
  462. <DirectionalLayout
  463.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  464.     ohos:height="match_parent"
  465.     ohos:width="match_parent"
  466.     ohos:alignment="center"
  467.     ohos:orientation="vertical">
  468.     <Text
  469.         ohos:id="$+id:text_helloworld"
  470.         ohos:height="match_content"
  471.         ohos:width="match_content"
  472.         ohos:background_element="$graphic:background_ability_main"
  473.         ohos:layout_alignment="horizontal_center"
  474.         ohos:text="itcyy第一个页面"
  475.         ohos:text_size="40vp"
  476.         />
  477.     <Button
  478.         ohos:id="$+id:cyy_one_button"
  479.         ohos:height="match_content"
  480.         ohos:width="match_content"
  481.         ohos:text_size="40fp"
  482.         ohos:background_element="red"
  483.         ohos:text="点我"/>
  484. </DirectionalLayout>    Operation operation = new Intent.OperationBuilder()<?xml version="1.0" encoding="utf-8"?>
  485. <DirectionalLayout
  486.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  487.     ohos:height="match_parent"
  488.     ohos:width="match_parent"
  489.     ohos:alignment="center"
  490.     ohos:orientation="vertical">
  491.     <Text
  492.         ohos:id="$+id:text_helloworld"
  493.         ohos:height="match_content"
  494.         ohos:width="match_content"
  495.         ohos:background_element="$graphic:background_ability_main"
  496.         ohos:layout_alignment="horizontal_center"
  497.         ohos:text="itcyy第一个页面"
  498.         ohos:text_size="40vp"
  499.         />
  500.     <Button
  501.         ohos:id="$+id:cyy_one_button"
  502.         ohos:height="match_content"
  503.         ohos:width="match_content"
  504.         ohos:text_size="40fp"
  505.         ohos:background_element="red"
  506.         ohos:text="点我"/>
  507. </DirectionalLayout><?xml version="1.0" encoding="utf-8"?>
  508. <DirectionalLayout
  509.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  510.     ohos:height="match_parent"
  511.     ohos:width="match_parent"
  512.     ohos:alignment="center"
  513.     ohos:orientation="vertical">
  514.     <Text
  515.         ohos:id="$+id:text_helloworld"
  516.         ohos:height="match_content"
  517.         ohos:width="match_content"
  518.         ohos:background_element="$graphic:background_ability_main"
  519.         ohos:layout_alignment="horizontal_center"
  520.         ohos:text="itcyy第一个页面"
  521.         ohos:text_size="40vp"
  522.         />
  523.     <Button
  524.         ohos:id="$+id:cyy_one_button"
  525.         ohos:height="match_content"
  526.         ohos:width="match_content"
  527.         ohos:text_size="40fp"
  528.         ohos:background_element="red"
  529.         ohos:text="点我"/>
  530. </DirectionalLayout>    .withDeviceId("")//要跳转到哪个设备上,如果传递一个空字符串,则表示跳转到本机<?xml version="1.0" encoding="utf-8"?>
  531. <DirectionalLayout
  532.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  533.     ohos:height="match_parent"
  534.     ohos:width="match_parent"
  535.     ohos:alignment="center"
  536.     ohos:orientation="vertical">
  537.     <Text
  538.         ohos:id="$+id:text_helloworld"
  539.         ohos:height="match_content"
  540.         ohos:width="match_content"
  541.         ohos:background_element="$graphic:background_ability_main"
  542.         ohos:layout_alignment="horizontal_center"
  543.         ohos:text="itcyy第一个页面"
  544.         ohos:text_size="40vp"
  545.         />
  546.     <Button
  547.         ohos:id="$+id:cyy_one_button"
  548.         ohos:height="match_content"
  549.         ohos:width="match_content"
  550.         ohos:text_size="40fp"
  551.         ohos:background_element="red"
  552.         ohos:text="点我"/>
  553. </DirectionalLayout><?xml version="1.0" encoding="utf-8"?>
  554. <DirectionalLayout
  555.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  556.     ohos:height="match_parent"
  557.     ohos:width="match_parent"
  558.     ohos:alignment="center"
  559.     ohos:orientation="vertical">
  560.     <Text
  561.         ohos:id="$+id:text_helloworld"
  562.         ohos:height="match_content"
  563.         ohos:width="match_content"
  564.         ohos:background_element="$graphic:background_ability_main"
  565.         ohos:layout_alignment="horizontal_center"
  566.         ohos:text="itcyy第一个页面"
  567.         ohos:text_size="40vp"
  568.         />
  569.     <Button
  570.         ohos:id="$+id:cyy_one_button"
  571.         ohos:height="match_content"
  572.         ohos:width="match_content"
  573.         ohos:text_size="40fp"
  574.         ohos:background_element="red"
  575.         ohos:text="点我"/>
  576. </DirectionalLayout>    .withBundleName("com.itcyy.myapplication")//要跳转到哪个应用上,小括号里可写包名<?xml version="1.0" encoding="utf-8"?>
  577. <DirectionalLayout
  578.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  579.     ohos:height="match_parent"
  580.     ohos:width="match_parent"
  581.     ohos:alignment="center"
  582.     ohos:orientation="vertical">
  583.     <Text
  584.         ohos:id="$+id:text_helloworld"
  585.         ohos:height="match_content"
  586.         ohos:width="match_content"
  587.         ohos:background_element="$graphic:background_ability_main"
  588.         ohos:layout_alignment="horizontal_center"
  589.         ohos:text="itcyy第一个页面"
  590.         ohos:text_size="40vp"
  591.         />
  592.     <Button
  593.         ohos:id="$+id:cyy_one_button"
  594.         ohos:height="match_content"
  595.         ohos:width="match_content"
  596.         ohos:text_size="40fp"
  597.         ohos:background_element="red"
  598.         ohos:text="点我"/>
  599. </DirectionalLayout><?xml version="1.0" encoding="utf-8"?>
  600. <DirectionalLayout
  601.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  602.     ohos:height="match_parent"
  603.     ohos:width="match_parent"
  604.     ohos:alignment="center"
  605.     ohos:orientation="vertical">
  606.     <Text
  607.         ohos:id="$+id:text_helloworld"
  608.         ohos:height="match_content"
  609.         ohos:width="match_content"
  610.         ohos:background_element="$graphic:background_ability_main"
  611.         ohos:layout_alignment="horizontal_center"
  612.         ohos:text="itcyy第一个页面"
  613.         ohos:text_size="40vp"
  614.         />
  615.     <Button
  616.         ohos:id="$+id:cyy_one_button"
  617.         ohos:height="match_content"
  618.         ohos:width="match_content"
  619.         ohos:text_size="40fp"
  620.         ohos:background_element="red"
  621.         ohos:text="点我"/>
  622. </DirectionalLayout>    .withAbilityName("com.itcyy.myapplication.cyy_two_Ability")//要跳转到哪个页面,小括号填写包名<?xml version="1.0" encoding="utf-8"?>
  623. <DirectionalLayout
  624.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  625.     ohos:height="match_parent"
  626.     ohos:width="match_parent"
  627.     ohos:alignment="center"
  628.     ohos:orientation="vertical">
  629.     <Text
  630.         ohos:id="$+id:text_helloworld"
  631.         ohos:height="match_content"
  632.         ohos:width="match_content"
  633.         ohos:background_element="$graphic:background_ability_main"
  634.         ohos:layout_alignment="horizontal_center"
  635.         ohos:text="itcyy第一个页面"
  636.         ohos:text_size="40vp"
  637.         />
  638.     <Button
  639.         ohos:id="$+id:cyy_one_button"
  640.         ohos:height="match_content"
  641.         ohos:width="match_content"
  642.         ohos:text_size="40fp"
  643.         ohos:background_element="red"
  644.         ohos:text="点我"/>
  645. </DirectionalLayout><?xml version="1.0" encoding="utf-8"?>
  646. <DirectionalLayout
  647.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  648.     ohos:height="match_parent"
  649.     ohos:width="match_parent"
  650.     ohos:alignment="center"
  651.     ohos:orientation="vertical">
  652.     <Text
  653.         ohos:id="$+id:text_helloworld"
  654.         ohos:height="match_content"
  655.         ohos:width="match_content"
  656.         ohos:background_element="$graphic:background_ability_main"
  657.         ohos:layout_alignment="horizontal_center"
  658.         ohos:text="itcyy第一个页面"
  659.         ohos:text_size="40vp"
  660.         />
  661.     <Button
  662.         ohos:id="$+id:cyy_one_button"
  663.         ohos:height="match_content"
  664.         ohos:width="match_content"
  665.         ohos:text_size="40fp"
  666.         ohos:background_element="red"
  667.         ohos:text="点我"/>
  668. </DirectionalLayout>    .build();//将上面三个信息打包<?xml version="1.0" encoding="utf-8"?>
  669. <DirectionalLayout
  670.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  671.     ohos:height="match_parent"
  672.     ohos:width="match_parent"
  673.     ohos:alignment="center"
  674.     ohos:orientation="vertical">
  675.     <Text
  676.         ohos:id="$+id:text_helloworld"
  677.         ohos:height="match_content"
  678.         ohos:width="match_content"
  679.         ohos:background_element="$graphic:background_ability_main"
  680.         ohos:layout_alignment="horizontal_center"
  681.         ohos:text="itcyy第一个页面"
  682.         ohos:text_size="40vp"
  683.         />
  684.     <Button
  685.         ohos:id="$+id:cyy_one_button"
  686.         ohos:height="match_content"
  687.         ohos:width="match_content"
  688.         ohos:text_size="40fp"
  689.         ohos:background_element="red"
  690.         ohos:text="点我"/>
  691. </DirectionalLayout>    //把打包之后的operation对象传到意图里<?xml version="1.0" encoding="utf-8"?>
  692. <DirectionalLayout
  693.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  694.     ohos:height="match_parent"
  695.     ohos:width="match_parent"
  696.     ohos:alignment="center"
  697.     ohos:orientation="vertical">
  698.     <Text
  699.         ohos:id="$+id:text_helloworld"
  700.         ohos:height="match_content"
  701.         ohos:width="match_content"
  702.         ohos:background_element="$graphic:background_ability_main"
  703.         ohos:layout_alignment="horizontal_center"
  704.         ohos:text="itcyy第一个页面"
  705.         ohos:text_size="40vp"
  706.         />
  707.     <Button
  708.         ohos:id="$+id:cyy_one_button"
  709.         ohos:height="match_content"
  710.         ohos:width="match_content"
  711.         ohos:text_size="40fp"
  712.         ohos:background_element="red"
  713.         ohos:text="点我"/>
  714. </DirectionalLayout>    i.setOperation(operation);<?xml version="1.0" encoding="utf-8"?>
  715. <DirectionalLayout
  716.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  717.     ohos:height="match_parent"
  718.     ohos:width="match_parent"
  719.     ohos:alignment="center"
  720.     ohos:orientation="vertical">
  721.     <Text
  722.         ohos:id="$+id:text_helloworld"
  723.         ohos:height="match_content"
  724.         ohos:width="match_content"
  725.         ohos:background_element="$graphic:background_ability_main"
  726.         ohos:layout_alignment="horizontal_center"
  727.         ohos:text="itcyy第一个页面"
  728.         ohos:text_size="40vp"
  729.         />
  730.     <Button
  731.         ohos:id="$+id:cyy_one_button"
  732.         ohos:height="match_content"
  733.         ohos:width="match_content"
  734.         ohos:text_size="40fp"
  735.         ohos:background_element="red"
  736.         ohos:text="点我"/>
  737. </DirectionalLayout>    //跳转页面<?xml version="1.0" encoding="utf-8"?>
  738. <DirectionalLayout
  739.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  740.     ohos:height="match_parent"
  741.     ohos:width="match_parent"
  742.     ohos:alignment="center"
  743.     ohos:orientation="vertical">
  744.     <Text
  745.         ohos:id="$+id:text_helloworld"
  746.         ohos:height="match_content"
  747.         ohos:width="match_content"
  748.         ohos:background_element="$graphic:background_ability_main"
  749.         ohos:layout_alignment="horizontal_center"
  750.         ohos:text="itcyy第一个页面"
  751.         ohos:text_size="40vp"
  752.         />
  753.     <Button
  754.         ohos:id="$+id:cyy_one_button"
  755.         ohos:height="match_content"
  756.         ohos:width="match_content"
  757.         ohos:text_size="40fp"
  758.         ohos:background_element="red"
  759.         ohos:text="点我"/>
  760. </DirectionalLayout>    startAbility(i);<?xml version="1.0" encoding="utf-8"?>
  761. <DirectionalLayout
  762.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  763.     ohos:height="match_parent"
  764.     ohos:width="match_parent"
  765.     ohos:alignment="center"
  766.     ohos:orientation="vertical">
  767.     <Text
  768.         ohos:id="$+id:text_helloworld"
  769.         ohos:height="match_content"
  770.         ohos:width="match_content"
  771.         ohos:background_element="$graphic:background_ability_main"
  772.         ohos:layout_alignment="horizontal_center"
  773.         ohos:text="itcyy第一个页面"
  774.         ohos:text_size="40vp"
  775.         />
  776.     <Button
  777.         ohos:id="$+id:cyy_one_button"
  778.         ohos:height="match_content"
  779.         ohos:width="match_content"
  780.         ohos:text_size="40fp"
  781.         ohos:background_element="red"
  782.         ohos:text="点我"/>
  783. </DirectionalLayout>}    }}
复制代码
看下效果


喜欢本博文的可以关注一下Gun尘心,以后会持续更新关于HarmonyOS APP 开发的基础知识的

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表