赞
踩
目录
鸿蒙学堂:https://hmxt.org/
(1)下载鸿蒙的IDE:DevEco Studio,安装过程一路next即可;
(2)安装好之后,新建工程,我这边使用Java进行开发,所以选择Empty Ability(Java),选择完成之后,点击Next:
(3)工程配置,主要配置一下设备类型,我这边选择了可穿戴设备:
(4)编译运行需要模拟器,在Tools-Device Manager中可以申请华为的云端资源进行模拟运行,这个过程中需要华为账号,以及一些身份认证等,按照流程完成之后,就会有设备可供选择操作,不过只有一小时的使用时间:
这边程序设计参考了官方提供的入门文档中的例程,主要要是可穿戴设备中的两个界面切换功能:
(1)设计两个界面:在图示路径下新建两个xml文件ability_main、ability_second,这两个文件用于界面布局,效果如下:
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"
- ohos:background_element="#000000">
-
- <Text
- ohos:id="$+id:text_helloworld"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:center_in_parent="true"
- ohos:text="Hello World"
- ohos:text_color="white"
- ohos:text_size="32fp"
- />
-
- <Button
- ohos:id="$+id:button1"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:text_size="19fp"
- ohos:text="Next"
- ohos:top_padding="8vp"
- ohos:bottom_padding="8vp"
- ohos:right_padding="80vp"
- ohos:left_padding="80vp"
- ohos:text_color="white"
- ohos:background_element="$graphic:background_ability_main"
- ohos:center_in_parent="true"
- ohos:align_parent_bottom="true"
- />
- </DirectionalLayout>
ability_second.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"
- ohos:background_element="#000000">
-
- <Text
- ohos:id="$+id:text_second"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:center_in_parent="true"
- ohos:text="Nice to eat you!"
- ohos:text_color="white"
- ohos:text_size="20fp"
- />
-
- <Button
- ohos:id="$+id:button2"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:text_size="19fp"
- ohos:text="Back"
- ohos:top_padding="8vp"
- ohos:bottom_padding="8vp"
- ohos:right_padding="80vp"
- ohos:left_padding="80vp"
- ohos:text_color="white"
- ohos:background_element="$graphic:background_ability_main"
- ohos:center_in_parent="true"
- ohos:align_parent_bottom="true"
- />
- </DirectionalLayout>
(2)加载布局:在图示路径下新建两个源文件MainAbility、SecondAbility,并通过重写onStart的方法加载布局:
MainAbility.java代码如下:
- package com.example.myapplication;
-
- import com.example.myapplication.slice.MainAbilitySlice;
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.content.Intent;
- import ohos.aafwk.content.Operation;
- import ohos.agp.components.*;
-
- public class MainAbility extends Ability {
-
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- }
-
- @Override
- public void onActive(){
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent){
- super.onForeground(intent);
- }
- }
SecondAbility.java代码如下:
- package com.example.myapplication;
-
- import com.example.myapplication.slice.SecondAbilitySlice;
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.content.Intent;
- import ohos.aafwk.content.Operation;
- import ohos.agp.colors.RgbColor;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.components.DependentLayout;
- import ohos.agp.components.DependentLayout.LayoutConfig;
- import ohos.agp.components.Text;
- import ohos.agp.components.element.ShapeElement;
- import ohos.agp.utils.Color;
-
- import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
- import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
-
- public class SecondAbility extends Ability {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_second);
- }
-
- @Override
- public void onActive(){
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent){
- super.onForeground(intent);
- }
- }
(3)点击交互:通过在MainAbility中点击Next或者在SecondAbility中点击Back实现两个界面的切换,这就需要重写onStart方法添加按钮响应逻辑:
MainAbility.java代码如下:
- package com.example.myapplication;
-
- import com.example.myapplication.slice.MainAbilitySlice;
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.content.Intent;
- import ohos.aafwk.content.Operation;
- import ohos.agp.components.*;
-
- public class MainAbility extends Ability {
-
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Button button1 = (Button)findComponentById(ResourceTable.Id_button1);
-
- if(button1 != null){
- //为按钮设置点击回调
- button1.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- Intent secondIntent = new Intent();
- //指定待启动FA的bundleName和abilityName
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.myapplication")
- .withAbilityName("com.example.myapplication.SecondAbility")
- .build();
- secondIntent.setOperation(operation);
- //通过AbilitySlice的startAbility接口实现启动另一个界面
- startAbility(secondIntent);
- }
- });
- }
- }
-
- @Override
- public void onActive(){
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent){
- super.onForeground(intent);
- }
- }
SecondAbility.java代码如下:
- package com.example.myapplication;
-
- import com.example.myapplication.slice.SecondAbilitySlice;
- import ohos.aafwk.ability.Ability;
- import ohos.aafwk.content.Intent;
- import ohos.aafwk.content.Operation;
- import ohos.agp.colors.RgbColor;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.components.DependentLayout;
- import ohos.agp.components.DependentLayout.LayoutConfig;
- import ohos.agp.components.Text;
- import ohos.agp.components.element.ShapeElement;
- import ohos.agp.utils.Color;
-
- import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
- import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
-
- public class SecondAbility extends Ability {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_second);
-
- Button button2 = (Button)findComponentById(ResourceTable.Id_button2);
-
- if(button2 != null){
- //为按钮设置点击回调
- button2.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- Intent mianIntent = new Intent();
- //指定待启动FA的bundleName和abilityName
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.example.myapplication")
- .withAbilityName("com.example.myapplication.MainAbility")
- .build();
- mianIntent.setOperation(operation);
- //通过AbilitySlice的startAbility接口实现启动另一个界面
- startAbility(mianIntent);
- }
- });
- }
- }
-
- @Override
- public void onActive(){
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent){
- super.onForeground(intent);
- }
- }
(4)模拟器测试:在之前申请到的模拟器上测试刚编译完成的程序,可以通过点击Next或者Back按钮实现两个界面的切换:
至此,初体验了一下华为鸿蒙IDE的使用和基于鸿蒙平台的简单程序开发过程,感觉跟Andrio Studio的使用差不多,要是熟悉安卓开发的朋友,应该能够比较轻松地接入基于鸿蒙的开发。
这边只是一个入门体验,后续还需持续学习。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。