Harmony应用开发学习(一)
1、HelloWorld
1.1 开发工具的项目结构
DevEco Studio基于IDEA开发,基本项目结构为:项目 —— 模块 —— 包 —— 类
我们主要关心的是HarmonyProject -> entry
在entry 中java 编写代码,resources 保存我们所需要的文件,config.json 关于APP的配置文件
1.2 HelloWorld
运行项目:
- 登录账号
- 选择并开启模拟器:
Tools -> Device Manager -> 选择设备 - 运行项目
页面中的包含关系:
-
Ability页面 -> AbilitySlice子页面(切片) -> 展示内容 -
一个Ability对应一个Hap包,每一个Hap包都可以单独下载安装,需要什么下载什么 -
在以后的开发中,一个单独的功能对应一个Ability。如果这个功能需要进行切换,就可以在Ability中,写多个子页面AbilitySlice进行切换
config.json配置文件:
- 项目配置app:厂商信息、项目版本号
- 应用在设备上的配置信息deviceConfig:应用运行时进程名、是否允许使用流量、是否支持未解锁时启动、权限
- 代码中配置module:所有的Ability
{
"app": {
"bundleName": "com.example.first",
"vendor": "example",
"version": {
"code": 1000000,
"name": "1.0.0"
}
},
"deviceConfig": {},
"module": {
"package": "com.example.first",
"name": ".MyApplication",
"mainAbility": "com.example.first.MainAbility",
"deviceType": [
"phone"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry",
"installationFree": false
},
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "unspecified",
"name": "com.example.first.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:entry_MainAbility",
"type": "page",
"launchType": "standard"
},
{
"orientation": "unspecified",
"name": "com.example.first.SecondAbility",
"icon": "$media:icon",
"description": "$string:secondability_description",
"label": "$string:entry_SecondAbility",
"type": "page",
"launchType": "standard"
}
]
}
}
2、页面跳转案例
在鸿蒙UI中,提供了两种编写布局的方式:
- XML文件:标签表示要展示的不同内容,
<Text> 文本、<Image> 图片、<Button> 按钮 - Java代码:对象表示要展示的不同内容,文本(Text对象)、图片(Image对象)、按钮(Button对象)
步骤:
- 创建第二个页面Ability页面
- 构建页面
- 编写实现代码
2.1 构建页面
第一个页面使用修改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:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="第一个页面"
ohos:text_size="40vp"
/>
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="red"
ohos:text_size="40fp"
ohos:text="点我"
/>
</DirectionalLayout>
第二个页面使用Java代码形式构建页面:
package com.example.first.slice;
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 SecondAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
DirectionalLayout dl = new DirectionalLayout(this);
Text text = new Text(this);
text.setText("第二个页面");
text.setTextSize(55);
text.setTextColor(Color.BLUE);
dl.addComponent(text);
super.setUIContent(dl);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
2.2 实现代码
package com.example.first.slice;
import com.example.first.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 btn;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
btn = (Button) findComponentById(ResourceTable.Id_but1);
btn.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
if (component == btn) {
Intent i = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.example.first")
.withAbilityName("com.example.first.SecondAbility")
.build();
i.setOperation(operation);
startAbility(i);
}
}
}
|