鸿蒙HarmonyOS北向应用开发者 极速入门教程(一)续-实战练习篇2

开发 OpenHarmony
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz

[[376968]]

 想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

Tablet 模拟器上的代码案例

新建项目选择模板进行下一步。


显示效果:


点击“点击了解更多”进入下一个页面


代码如下

第一个页面的布局

  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:orientation="vertical"
  7.  
  8.     <DirectionalLayout 
  9.         ohos:width="match_parent" 
  10.         ohos:weight="1"
  11.         <Text 
  12.             ohos:width="match_content" 
  13.             ohos:height="match_content" 
  14.             ohos:text="蛟龙腾飞" 
  15.             ohos:text_size="40fp" 
  16.             ohos:top_margin="180px" 
  17.             ohos:left_margin="80px"/> 
  18.  
  19.     </DirectionalLayout> 
  20.     <DirectionalLayout 
  21.         ohos:width="match_parent" 
  22.         ohos:weight="4" 
  23.         ohos:orientation="horizontal"
  24.         <DirectionalLayout 
  25.             ohos:height="match_parent" 
  26.             ohos:weight="1"
  27.             <Image 
  28.                 ohos:width="600px" 
  29.                 ohos:height="600px" 
  30.                 ohos:top_margin="160px" 
  31.                 ohos:left_margin="100px" 
  32.                 ohos:image_src="$media:index6"/> 
  33.         </DirectionalLayout> 
  34.         <DirectionalLayout 
  35.             ohos:height="match_parent" 
  36.             ohos:weight="2"
  37.             <Text 
  38.                 ohos:width="match_content" 
  39.                 ohos:height="match_content" 
  40.                 ohos:multiple_lines="true" 
  41.                 ohos:text="带你从传统的互联网、移动互联网时代 
  42.                                跳转到万物互联的智能世界! 
  43.                                           马上创建体验个人、公司或组织的鸿蒙应用吧!" 
  44.                 ohos:text_size="35fp" 
  45.                 ohos:top_margin="240px" 
  46.                 ohos:left_margin="60px"/> 
  47.             <Button 
  48.                 ohos:id="$+id:jltfbutton" 
  49.                 ohos:width="match_content" 
  50.                 ohos:height="match_content" 
  51.                 ohos:text="点击了解更多" 
  52.                 ohos:text_size="40fp" 
  53.                 ohos:top_margin="380px" 
  54.                 ohos:left_margin="1000px" 
  55.                 ohos:text_color="red"/> 
  56.         </DirectionalLayout> 
  57.  
  58.     </DirectionalLayout> 
  59.  
  60. </DirectionalLayout> 

 第二个页面的布局

  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:orientation="vertical"
  7.  
  8.     <DirectionalLayout 
  9.         ohos:width="match_parent" 
  10.        ohos:weight="1" 
  11.         > 
  12.         <Text 
  13.             ohos:width="match_content" 
  14.             ohos:height="match_content" 
  15.             ohos:text="蛟龙腾飞" 
  16.             ohos:text_size="40fp" 
  17.             ohos:top_margin="180px" 
  18.             ohos:left_margin="80px"/> 
  19.  
  20.     </DirectionalLayout> 
  21.     <DirectionalLayout 
  22.         ohos:width="match_parent" 
  23.         ohos:weight="4" 
  24.         ohos:orientation="horizontal"
  25.         <DirectionalLayout 
  26.             ohos:height="match_parent" 
  27.             ohos:weight="1"
  28.             <Image 
  29.                 ohos:width="600px" 
  30.                 ohos:height="600px" 
  31.                 ohos:top_margin="60px" 
  32.                 ohos:left_margin="100px" 
  33.                 ohos:image_src="$media:index7"/> 
  34.         </DirectionalLayout> 
  35.         <DirectionalLayout 
  36.             ohos:height="match_parent" 
  37.             ohos:weight="2"
  38.             <Text 
  39.                 ohos:width="match_content" 
  40.                 ohos:height="match_content" 
  41.                 ohos:multiple_lines="true" 
  42.                 ohos:text="你好,欢迎来到鸿蒙时代! 
  43.                                     蛟龙腾飞,鸿蒙先行者,全心全意为你提供鸿蒙各项服务!" 
  44.                 ohos:text_size="35fp" 
  45.                 ohos:top_margin="240px" 
  46.                 ohos:left_margin="10px"/> 
  47.         </DirectionalLayout> 
  48.  
  49.     </DirectionalLayout> 
  50.  
  51. </DirectionalLayout> 

 然后再第一个页面中添加点击事件进行跳转

  1. Button button = (Button) findComponentById(ResourceTable.Id_jltfbutton); 
  2.  
  3. button.setClickedListener(new Component.ClickedListener() { 
  4.     @Override 
  5.     public void onClick(Component component) { 
  6.  
  7.         present(new jltftabletSlice(),new Intent()); 
  8.     } 
  9.  
  10.  
  11. }); 

 Lite wearable模拟器上的代码案例

新建项目选择模板进行下一步。


显示效果:


点击“点击了解更多”进入下一个页面


代码如下:

Index.hml

  1. <div class="container"
  2.     <text class="jltftxt"
  3.        蛟龙腾飞 
  4.     </text> 
  5.     <image src="/common/index22.jpg" class="jltfimg"/> 
  6.     <text class="jltftxt1"
  7.         带你从传统的互联网、移动互 
  8.     </text> 
  9.     <text class="jltftxt2"
  10.         联网时代. 跳转到万物互联的智 
  11.     </text> 
  12.     <text class="jltftxt3"
  13.          能世界!。马上创建体验个人、 
  14.     </text> 
  15.     <text class="jltftxt4"
  16.         公司或组织的鸿蒙应用吧! 
  17.     </text> 
  18.     <input class="jltfbtn"type="button" value="点击了解更多" onclick="jltfclick"/> 
  19. </div> 

 Index.css

  1. .container { 
  2.     flex-direction: column
  3.     width: 454px; 
  4.     height: 454px; 
  5.     justify-content: center; 
  6.     align-items: center; 
  7. .jltftxt { 
  8.      margin-left: -180px; 
  9.      margin-bottom: 10px; 
  10. .jltftxt1 { 
  11.     width: 400px; 
  12.     font-size: 30px; 
  13.  
  14. .jltfimg { 
  15.     width: 138px; 
  16.     height:103px; 
  17.     margin-left: 5px; 
  18. .jltfbtn { 
  19.     width: 200px; 
  20.     margin-bottom: -70px; 

 Index.js

  1. import router from '@system.router' 
  2.  
  3. export default { 
  4.     data: { 
  5.  
  6.     }, 
  7.     jltfclick() { 
  8.         router.replace({ 
  9.             uri:'pages/jltfindex/jltfindex' 
  10.         }); 
  11.     } 

 Jltfindex.hml

  1. <div class="container"
  2.     <text class="jltftxt"
  3.         蛟龙腾飞 
  4.     </text> 
  5.     <image src="/common/index16.jpg" class="jltfimg"/> 
  6.     <text class="jltftxt1"
  7.         你好,欢迎来到鸿蒙时代! 
  8.     </text> 
  9.     <text class="jltftxt2"
  10.         蛟龙腾飞,鸿蒙先行者,全心全 
  11.     </text> 
  12.     <text class="jltftxt3"
  13.         意为你提供鸿蒙各项服务!        
  14.  
  15. </div> 

 Jltfindex.css

  1. .container { 
  2.     flex-direction: column
  3.     width: 454px; 
  4.     height: 454px; 
  5.     justify-content: center; 
  6.     align-items: center; 
  7. .jltftxt { 
  8.     margin-left: -180px; 
  9.     margin-bottom: 10px; 
  10. .jltftxt1 { 
  11.     width: 400px; 
  12.     font-size: 30px; 
  13.  
  14. .jltfimg { 
  15.     width: 413px; 
  16.     height:103px; 
  17.     margin-left: 8px; 
  18. .jltfbtn { 
  19.     width: 200px; 
  20.     margin-bottom: -70px; 

 Jltfindex.js

  1. export default { 
  2.     data: { 
  3.     } 

 在第一个页面中添加跳转的代码

在第一个页面js文件中加入如下代码即可

  1. jltfclick() { 
  2.     router.replace({ 
  3.         uri:'pages/jltfindex/jltfindex' 
  4.     }); 

 2. 练习题二

还是基于“练习一”题目中的图片和文字内容,7种设备中用其他不同的模板样式尝试实现一下和“练习一”模板样式不一样的效果,尽可能每种设备实现一种不同的布局和效果呈现,作业要求,样式截图与全部代码。

3. 点击和尝试DevEco Studio里的每个菜单栏,概览这个DevEco Studio的各项功能结构。结合前面的练习,写一个100字以上的Deveco Studio体验心得。

具体编辑器使用的细节可以参考如下链接和内容。

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/tools_overview-0000001053582387


深圳市蛟龙腾飞网络科技有限公司

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-01-19 10:09:02

鸿蒙HarmonyOS应用

2021-01-18 13:26:06

鸿蒙HarmonyOS应用

2023-07-31 17:35:31

ArkTS鸿蒙

2017-06-30 09:47:45

PythonJava开发者

2023-08-04 15:00:43

ArkTS语言鸿蒙

2024-01-03 15:31:16

网格布局ArkTSGrid

2022-08-12 08:02:11

Solid.jsReact

2023-04-11 09:12:31

北向应用开发鸿蒙

2020-11-09 11:56:49

HarmonyOS

2021-08-03 14:57:25

华为HarmonyOS人才建设

2021-01-11 11:04:49

鸿蒙HarmonyOS应用开发

2023-01-16 16:06:34

操作系统鸿蒙

2021-02-07 12:08:39

鸿蒙HarmonyOS应用开发

2022-08-09 16:01:24

应用开发鸿蒙

2021-10-14 09:58:24

消息中间件ActiveMQ Java

2011-07-21 10:29:18

iPhone 开发

2010-06-13 09:45:35

Widget开发
点赞
收藏

51CTO技术栈公众号