从零开始学习HarmonyOS应用开发——1

1、首先安装必要的开发工具

1.1通过访问https://developer.harmonyos.com/cn/develop/deveco-studio在这里插入图片描述
1.2安装软件
在这里插入图片描述
1.3通过访问https://nodejs.org/en/download/
(注:安装Node.js按自己系统安装)
在这里插入图片描述
1.4安装HarmonyOS SDK
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
把上述都勾选中进行安装。

2、创建一个HarmonyOS项目

在这里插入图片描述
选择EmptyAbility(选择框中的内容是,支持的设备)
在这里插入图片描述
下一步
在这里插入图片描述

3、实现最基础的页面跳转

3.1首先进入目录下的entry/src/main/resources/base/layout/下的ability_main.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="$string:mainability_HelloWorld"
        ohos:text_size="40vp"
        />
    <Button
        ohos:id="$+id:button1"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:layout_alignment="horizontal_center"
        ohos:text="跳转按钮"
        ohos:text_size="50vp"></Button>

</DirectionalLayout>

3.2然后去目录entry/src/main/java/com.example.helloword/slice/下的MainAbilitySlice文件:

package com.example.helloword.slice;

import com.example.helloword.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        Button button = (Button) findComponentById(ResourceTable.Id_button1);/*注释:在找组件的过程中,系统会自己加上ID_,后面的
        部分为自己取的名字*/
        button.setClickedListener(listener->present(new SecondAbilitySlice(),new Intent()));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

3.3创建一个Slice在目录entry/src/main/java/com.example.helloword/slice/下新建一个java class取名为SecondAbilitySlice
SecondAbilitySlice文件为:

package com.example.helloword.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class SecondAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        // 声明布局
        DependentLayout myLayout = new DependentLayout(this);

        // 设置布局宽高
        myLayout.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        myLayout.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);

        // 设置布局背景为白色
        ShapeElement background = new ShapeElement();
        background.setRgbColor(new RgbColor(255, 255, 255));
        myLayout.setBackground(background);

        // 创建一个文本
        Text text = new Text(this);
        text.setText("Hi there");
        text.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        text.setTextSize(100);
        text.setTextColor(Color.BLACK);

        // 设置文本的布局
        DependentLayout.LayoutConfig textConfig = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);
        textConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
        text.setLayoutConfig(textConfig);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }
}

3.4创建一个虚拟机
在这里插入图片描述
此处需要登陆,需要拥有开发者账号

选择手机(phone)

运行那个小三角小三角就好了

4、结语

好的开始是成功的一大步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值