HarmonyOS Next 应用开辟快速入门案例

打印 上一主题 下一主题

主题 1015|帖子 1015|积分 3045

项目代码gitee地址
     ( https://gitee.com/li-yangshui-and-jiaolong/HarmonyOS-Next-App/tree/master/MyApplication)
     开源协议使用:Apache License 2.0 ,代码包支持免费使用,可举行二次开辟后选择开源或闭源。
     一、创建项目
     1.创建项目,选择Application
     

     2.配置项目
     

     3.在AppScope文件下修改自定义项目配置
     

     在resources>base>element>string.json中修改“app_name”值,该值表现“应用名称”。
     

     在app.json5中修改“vender”值,该值表现“应用程序供应商”。
     4.在项目下的resource>base>media下添加图片
     

     二、创建卡片widget
     1.创建微、小、中、大卡片
     

     

     

     2.依次创建卡片
     

     

     

     3.卡片创建完成,修改卡片配置代码
     

     4.卡片代码如下:
     widget_wk代码:
                                   登录后复制                        
  1. @Entry
  2. @Component
  3. struct Widget_wkCard {
  4.   /*
  5.    * The title.
  6.    */
  7.   readonly TITLE: string = 'Hello World';
  8.   /*
  9.    * The action type.
  10.    */
  11.   readonly ACTION_TYPE: string = 'router';
  12.   /*
  13.    * The ability name.
  14.    */
  15.   readonly ABILITY_NAME: string = 'EntryAbility';
  16.   /*
  17.    * The message.
  18.    */
  19.   readonly MESSAGE: string = 'add detail';
  20.   /*
  21.    * The with percentage setting.
  22.    */
  23.   readonly FULL_WIDTH_PERCENT: string = '100%';
  24.   /*
  25.    * The height percentage setting.
  26.    */
  27.   readonly FULL_HEIGHT_PERCENT: string = '100%';
  28.   build() {
  29.     Row() {
  30.       Image($r("app.media.jltf")).width(28)
  31.       Text("你好,鸿蒙元服务!").fontSize(12).fontWeight(600)
  32.     }
  33.     .justifyContent(FlexAlign.Center)
  34.     .width(this.FULL_WIDTH_PERCENT)
  35.     .height(this.FULL_HEIGHT_PERCENT)
  36. .backgroundColor("#ff8fc7ff")//添加背景色
  37.     .onClick(() => {
  38.       postCardAction(this, {
  39.         "action": this.ACTION_TYPE,
  40.         "abilityName": this.ABILITY_NAME,
  41.         "params": {
  42.           "message": this.MESSAGE
  43.         }
  44.       });
  45.     })
  46.   }
  47. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
                       widget代码
                                   登录后复制                        
  1. @Entry
  2. @Component
  3. struct WidgetCard {
  4.   /*
  5.    * The max lines.
  6.    */
  7.   readonly MAX_LINES: number = 1;
  8.   /*
  9.    * The action type.
  10.    */
  11.   readonly ACTION_TYPE: string = 'router';
  12.   /*
  13.    * The message.
  14.    */
  15.   readonly MESSAGE: string = 'add detail';
  16.   /*
  17.    * The ability name.
  18.    */
  19.   readonly ABILITY_NAME: string = 'EntryAbility';
  20.   /*
  21.    * The with percentage setting.
  22.    */
  23.   readonly FULL_WIDTH_PERCENT: string = '100%';
  24.   /*
  25.    * The height percentage setting.
  26.    */
  27.   readonly FULL_HEIGHT_PERCENT: string = '100%';
  28.   build() {
  29.     Column() {
  30.       Image($r("app.media.jltf")).width(80)
  31.       Text("你好,鸿蒙元服务!").fontSize(12).fontWeight(600)
  32.         .margin({top:10})
  33.     }
  34.     .width(this.FULL_WIDTH_PERCENT)
  35.     .height(this.FULL_HEIGHT_PERCENT)
  36.     .justifyContent(FlexAlign.Center)
  37.     .alignItems(HorizontalAlign.Center)
  38. .backgroundColor("#ff8fc7ff")//添加背景色
  39.     .onClick(() => {
  40.       postCardAction(this, {
  41.         "action": this.ACTION_TYPE,
  42.         "abilityName": this.ABILITY_NAME,
  43.         "params": {
  44.           "message": this.MESSAGE
  45.         }
  46.       });
  47.     })
  48.   }
  49. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
                       widget_zk代码
                                   登录后复制                        
  1. @Entry
  2. @Component
  3. struct Widget_zkCard {
  4.   /*
  5.    * The title.
  6.    */
  7.   readonly TITLE: string = 'Hello World';
  8.   /*
  9.    * The action type.
  10.    */
  11.   readonly ACTION_TYPE: string = 'router';
  12.   /*
  13.    * The ability name.
  14.    */
  15.   readonly ABILITY_NAME: string = 'EntryAbility';
  16.   /*
  17.    * The message.
  18.    */
  19.   readonly MESSAGE: string = 'add detail';
  20.   /*
  21.    * The with percentage setting.
  22.    */
  23.   readonly FULL_WIDTH_PERCENT: string = '100%';
  24.   /*
  25.    * The height percentage setting.
  26.    */
  27.   readonly FULL_HEIGHT_PERCENT: string = '100%';
  28.   build() {
  29.     Column() {
  30.       Image($r("app.media.jltf")).width(80)
  31.       Text("你好,鸿蒙元服务!").fontSize(16).fontWeight(600)
  32.         .margin({top:10})
  33.     }
  34.     .width(this.FULL_WIDTH_PERCENT)
  35.     .height(this.FULL_HEIGHT_PERCENT)
  36.     .justifyContent(FlexAlign.Center)
  37.     .alignItems(HorizontalAlign.Center)
  38. .backgroundColor("#ff8fc7ff")//添加背景色
  39.     .onClick(() => {
  40.       postCardAction(this, {
  41.         "action": this.ACTION_TYPE,
  42.         "abilityName": this.ABILITY_NAME,
  43.         "params": {
  44.           "message": this.MESSAGE
  45.         }
  46.       });
  47.     })
  48.   }
  49. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
                       widget_dk代码:
                                   登录后复制                        
  1. @Entry
  2. @Component
  3. struct Widget_dkCard {
  4.   /*
  5.    * The title.
  6.    */
  7.   readonly TITLE: string = 'Hello World';
  8.   /*
  9.    * The action type.
  10.    */
  11.   readonly ACTION_TYPE: string = 'router';
  12.   /*
  13.    * The ability name.
  14.    */
  15.   readonly ABILITY_NAME: string = 'EntryAbility';
  16.   /*
  17.    * The message.
  18.    */
  19.   readonly MESSAGE: string = 'add detail';
  20.   /*
  21.    * The with percentage setting.
  22.    */
  23.   readonly FULL_WIDTH_PERCENT: string = '100%';
  24.   /*
  25.    * The height percentage setting.
  26.    */
  27.   readonly FULL_HEIGHT_PERCENT: string = '100%';
  28.   build() {
  29.     Column() {
  30.       Image($r("app.media.jltf")).width(150)
  31.       Text("你好,鸿蒙元服务!").fontSize(20).fontWeight(600)
  32.         .margin({top:10})
  33.     }
  34.     .width(this.FULL_WIDTH_PERCENT)
  35.     .height(this.FULL_HEIGHT_PERCENT)
  36.     .justifyContent(FlexAlign.Center)
  37.     .alignItems(HorizontalAlign.Center)
  38. .backgroundColor("#ff8fc7ff")//添加背景色
  39.     .onClick(() => {
  40.       postCardAction(this, {
  41.         "action": this.ACTION_TYPE,
  42.         "abilityName": this.ABILITY_NAME,
  43.         "params": {
  44.           "message": this.MESSAGE
  45.         }
  46.       });
  47.     })
  48.   }
  49. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
                       三、创建应用page
1.修改pages/Index.ets中的代码,代码如下:
                                   登录后复制                        
  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State title: string = '蛟龙腾飞欢迎您'
  6.   build() {
  7.     Row() {
  8.       Column() {
  9.         //Image组件,展示logo
  10.         Image($r("app.media.jltf")).width(150).borderRadius(12)
  11.         //Text组件,展示文字详情
  12.         Text(this.title)
  13.           .fontSize(24)
  14.           .fontWeight(FontWeight.Bold)
  15.           .margin(10)
  16.         //Button组件,跳转下一页
  17.         Button("下一步").type(ButtonType.Capsule)
  18.           .onClick(()=>{
  19.             router.pushUrl({
  20.               url:"pages/test"
  21.             })
  22.           })
  23.       }
  24.       .width('100%')
  25.     }
  26.     .height('100%')
  27.     .backgroundColor("#ff8fc7ff")//添加背景色
  28.   }
  29. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
                       2.创建新的page
     

     

     

     3.新的page写入代码如下:
                                   登录后复制                        
  1. import router from '@ohos.router'
  2. @Entry
  3. @Component
  4. struct Test {
  5.   @State text1: string = '鸿蒙原生应用'
  6.   @State text2: string = '快速上手练习'
  7.   build() {
  8.     Row() {
  9.       Column() {
  10.         Text(this.text1)
  11.           .fontSize(30)
  12.           .fontWeight(FontWeight.Bold)
  13.         Text(this.text2)
  14.           .fontSize(30)
  15.           .fontWeight(FontWeight.Bold)
  16.           .margin(10)
  17.         //Button组件,返回上一页
  18.         Button("返回").type(ButtonType.Capsule)
  19.           .onClick(()=>{
  20.             router.back()
  21.           })
  22.       }
  23.       .width('100%')
  24.     }
  25.     .height('100%')
  26.     .backgroundColor("#ff8fc7ff")//添加背景色
  27.   }
  28. }
复制代码
      

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
                       四、项目结果
     1.预览器结果
     Widget:
     

     

     !
     Page:
     

     五、更换为OpenHarmony应用
     打开entry下的build-profile.json5文件,更换“targets”字段中的“runtimeOS”属性值,点击右上方的Sync Now同步就。
     

     


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表