当前位置:   article > 正文

【鸿蒙】鸿蒙操作系统应用开发入门级初体验_mainability.java

mainability.java

目录

 

一 鸿蒙相关资源

二 创建新工程

三 程序设计


鸿蒙相关资源

鸿蒙学堂: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代码如下:

  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. ohos:background_element="#000000">
  9. <Text
  10. ohos:id="$+id:text_helloworld"
  11. ohos:height="match_content"
  12. ohos:width="match_content"
  13. ohos:center_in_parent="true"
  14. ohos:text="Hello World"
  15. ohos:text_color="white"
  16. ohos:text_size="32fp"
  17. />
  18. <Button
  19. ohos:id="$+id:button1"
  20. ohos:width="match_content"
  21. ohos:height="match_content"
  22. ohos:text_size="19fp"
  23. ohos:text="Next"
  24. ohos:top_padding="8vp"
  25. ohos:bottom_padding="8vp"
  26. ohos:right_padding="80vp"
  27. ohos:left_padding="80vp"
  28. ohos:text_color="white"
  29. ohos:background_element="$graphic:background_ability_main"
  30. ohos:center_in_parent="true"
  31. ohos:align_parent_bottom="true"
  32. />
  33. </DirectionalLayout>

ability_second.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. ohos:background_element="#000000">
  9. <Text
  10. ohos:id="$+id:text_second"
  11. ohos:height="match_content"
  12. ohos:width="match_content"
  13. ohos:center_in_parent="true"
  14. ohos:text="Nice to eat you!"
  15. ohos:text_color="white"
  16. ohos:text_size="20fp"
  17. />
  18. <Button
  19. ohos:id="$+id:button2"
  20. ohos:width="match_content"
  21. ohos:height="match_content"
  22. ohos:text_size="19fp"
  23. ohos:text="Back"
  24. ohos:top_padding="8vp"
  25. ohos:bottom_padding="8vp"
  26. ohos:right_padding="80vp"
  27. ohos:left_padding="80vp"
  28. ohos:text_color="white"
  29. ohos:background_element="$graphic:background_ability_main"
  30. ohos:center_in_parent="true"
  31. ohos:align_parent_bottom="true"
  32. />
  33. </DirectionalLayout>

(2)加载布局:在图示路径下新建两个源文件MainAbility、SecondAbility,并通过重写onStart的方法加载布局:

MainAbility.java代码如下:

  1. package com.example.myapplication;
  2. import com.example.myapplication.slice.MainAbilitySlice;
  3. import ohos.aafwk.ability.Ability;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.components.*;
  7. public class MainAbility extends Ability {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_ability_main);
  12. }
  13. @Override
  14. public void onActive(){
  15. super.onActive();
  16. }
  17. @Override
  18. public void onForeground(Intent intent){
  19. super.onForeground(intent);
  20. }
  21. }

SecondAbility.java代码如下:

  1. package com.example.myapplication;
  2. import com.example.myapplication.slice.SecondAbilitySlice;
  3. import ohos.aafwk.ability.Ability;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.colors.RgbColor;
  7. import ohos.agp.components.Button;
  8. import ohos.agp.components.Component;
  9. import ohos.agp.components.DependentLayout;
  10. import ohos.agp.components.DependentLayout.LayoutConfig;
  11. import ohos.agp.components.Text;
  12. import ohos.agp.components.element.ShapeElement;
  13. import ohos.agp.utils.Color;
  14. import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
  15. import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
  16. public class SecondAbility extends Ability {
  17. @Override
  18. public void onStart(Intent intent) {
  19. super.onStart(intent);
  20. super.setUIContent(ResourceTable.Layout_ability_second);
  21. }
  22. @Override
  23. public void onActive(){
  24. super.onActive();
  25. }
  26. @Override
  27. public void onForeground(Intent intent){
  28. super.onForeground(intent);
  29. }
  30. }

(3)点击交互:通过在MainAbility中点击Next或者在SecondAbility中点击Back实现两个界面的切换,这就需要重写onStart方法添加按钮响应逻辑:

MainAbility.java代码如下:

  1. package com.example.myapplication;
  2. import com.example.myapplication.slice.MainAbilitySlice;
  3. import ohos.aafwk.ability.Ability;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.components.*;
  7. public class MainAbility extends Ability {
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_ability_main);
  12. Button button1 = (Button)findComponentById(ResourceTable.Id_button1);
  13. if(button1 != null){
  14. //为按钮设置点击回调
  15. button1.setClickedListener(new Component.ClickedListener() {
  16. @Override
  17. public void onClick(Component component) {
  18. Intent secondIntent = new Intent();
  19. //指定待启动FA的bundleName和abilityName
  20. Operation operation = new Intent.OperationBuilder()
  21. .withDeviceId("")
  22. .withBundleName("com.example.myapplication")
  23. .withAbilityName("com.example.myapplication.SecondAbility")
  24. .build();
  25. secondIntent.setOperation(operation);
  26. //通过AbilitySlice的startAbility接口实现启动另一个界面
  27. startAbility(secondIntent);
  28. }
  29. });
  30. }
  31. }
  32. @Override
  33. public void onActive(){
  34. super.onActive();
  35. }
  36. @Override
  37. public void onForeground(Intent intent){
  38. super.onForeground(intent);
  39. }
  40. }

SecondAbility.java代码如下:

  1. package com.example.myapplication;
  2. import com.example.myapplication.slice.SecondAbilitySlice;
  3. import ohos.aafwk.ability.Ability;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.colors.RgbColor;
  7. import ohos.agp.components.Button;
  8. import ohos.agp.components.Component;
  9. import ohos.agp.components.DependentLayout;
  10. import ohos.agp.components.DependentLayout.LayoutConfig;
  11. import ohos.agp.components.Text;
  12. import ohos.agp.components.element.ShapeElement;
  13. import ohos.agp.utils.Color;
  14. import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
  15. import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
  16. public class SecondAbility extends Ability {
  17. @Override
  18. public void onStart(Intent intent) {
  19. super.onStart(intent);
  20. super.setUIContent(ResourceTable.Layout_ability_second);
  21. Button button2 = (Button)findComponentById(ResourceTable.Id_button2);
  22. if(button2 != null){
  23. //为按钮设置点击回调
  24. button2.setClickedListener(new Component.ClickedListener() {
  25. @Override
  26. public void onClick(Component component) {
  27. Intent mianIntent = new Intent();
  28. //指定待启动FA的bundleName和abilityName
  29. Operation operation = new Intent.OperationBuilder()
  30. .withDeviceId("")
  31. .withBundleName("com.example.myapplication")
  32. .withAbilityName("com.example.myapplication.MainAbility")
  33. .build();
  34. mianIntent.setOperation(operation);
  35. //通过AbilitySlice的startAbility接口实现启动另一个界面
  36. startAbility(mianIntent);
  37. }
  38. });
  39. }
  40. }
  41. @Override
  42. public void onActive(){
  43. super.onActive();
  44. }
  45. @Override
  46. public void onForeground(Intent intent){
  47. super.onForeground(intent);
  48. }
  49. }

(4)模拟器测试:在之前申请到的模拟器上测试刚编译完成的程序,可以通过点击Next或者Back按钮实现两个界面的切换:

至此,初体验了一下华为鸿蒙IDE的使用和基于鸿蒙平台的简单程序开发过程,感觉跟Andrio Studio的使用差不多,要是熟悉安卓开发的朋友,应该能够比较轻松地接入基于鸿蒙的开发。

这边只是一个入门体验,后续还需持续学习。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/257708
推荐阅读
相关标签
  

闽ICP备14008679号