HarmonyOS开辟(ArkUI简单使用)

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

一、开辟准备

1.官网 

https://developer.huawei.com/consumer/cn/
2.工具

DevEco Studio
下载: 下载中心 | 华为开辟者同盟-HarmonyOS开辟者官网,共建鸿蒙生态

3.安装


4.开辟组件ArkTs

ArkTS是HarmonyOS主力应用开辟语言。它在TypeScript(简称TS)的基础上,匹配ArkUI框架,扩展了声明式UI、状态管理等相应的本领,让开辟者以更简便、更自然的方式开辟跨端应用。
5.快速上手


 


 6.ArkTS工程目次布局(Stage模子)



7.安装模仿器





 

 
解决步伐
首先打开控制面板 > 程序 > 程序与功能 > 启动或关闭Windows功能,找到并勾选“Hyper-V”、“Windows虚拟机监控程序平台”、“虚拟机平台”,点击确定并重启电脑。若勾选后启动模仿器仍然提示该错误,必要以管理员权限打开命令行窗口并实行:`bcdedit /set hypervisorlaunchtype auto`并重启电脑。
二、ArkUI

ArkUI(方舟UI框架)为应用的UI开辟提供了完备的基础设施,包括简便的UI语法、丰富的UI功能(组件、布局、动画以及交互事件),以及实时界面预览工具等,可以支持开辟者举行可视化界面开辟。
1.图片表现组件

a.声明Image组件并设置图片源 

  1. Image(src:string|PixelMap|Resource)
  2. Image('https://xxx.png')
  3. Image(PixelMapObject)
  4. Image($r('app.media.mate60'))
  5. Image($rawfile('mate60.png'))
复制代码
string格式,通常用来加载网络图片,必要申请网络访问权限:obos.permission.INTERNET
PixelMap格式,可以加载像素图,常用在图片编辑器中
Resource格式,加载本地图片,推荐使用
b.添加图片属性

  1. Image($r('app.media.icon'))
  2.     .width(100)
  3.     .height(120)
  4.     .borderRadius(10)
  5.     .interpolation(ImageInterpolation.High)
复制代码
2.文本表现组件

a.声明Text组件并设置文本内容

  1. Text(content?:string|Resource)
  2. Text('图片宽度')
  3. Text($r('app.string.width_label'))
复制代码
string格式,直接写文本内容
Resource格式,读取本地资源文件
 b.添加图片属性

  1. Text('注册账号')
  2.     .lineHeight(32)
  3.     .fontSize(20)
  4.     .fontColor('#ff1876f8')
  5.     .fontWeight(FontWeight.Medium)
复制代码
 3.文本输入框

a.声明TextInput组件

  1. TextInput({placeHolder?:ResourceStr,text?:ResourceStr})
  2. TextInput({placeHolder:'请输入账号或手机号'})
复制代码
placeHolder输入框无输入时的提示文本
text输入框当前的文本内容
 b.添加属性和事件

  1. TextInput({text:'请输入账号或手机号'})
  2.     .width(150)
  3.     .height(30)
  4.     .backgroundColor('#fff')
  5.     .type(InputType.Password)
  6.     .onChange(value=>{console.log(value)})
复制代码
 4.按钮输入框

a.声明Button组件,label是按钮文字

  1. Button(label?:ResourceStr)
  2. Button('点我')
  3. Button(){
  4.     Image($r('app.media.search))
  5.         .width(20)
  6.         .margin(10)
  7. }
复制代码
文字型按钮
自界说按钮,在Button组件内嵌套其他组件
 b.添加属性和事件

  1. Button('点我')
  2.     .width(100)
  3.     .height(30)
  4.     .type(ButtonType.Normal)
  5.     .onClick(()=>{})
复制代码
 5.滑动条组件

  1. Slider(options?:SilderOptions)
  2. Slider({
  3.      min:10,
  4.      max:100,
  5.      value:30,
  6.      step:10,
  7.      style:SliderStyle.OutSet,
  8.      direction:Axis.Horizontal,
  9.      reverse:false
  10. })
  11.      .width('90%')
  12.      .showTips(true)
  13.      .blockColor('#36d')
  14.      .onChange(()=>{})
复制代码
6.布局组件

Column容器从上往下分列
Row容器从左往右分列
间距:Space
7.示例


  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State imageWidth: number = 250;
  5.   build() {
  6.       Column() {
  7.         Row(){
  8.           Image('https://tse4-mm.cn.bing.net/th?id=OIP.nweYnRD-9eFP3nNgNDU9gwHaGt&pid=1.7&w=217&h=198&c=8&dpr=0.9')
  9.             .width(this.imageWidth)
  10.         }
  11.         .width('100%')
  12.         .height(400)
  13.         .justifyContent(FlexAlign.Center)
  14.         Row(){
  15.           Text($r('app.string.width_label'))
  16.             .fontSize(20)
  17.             .fontWeight(FontWeight.Bold)
  18.           TextInput({ placeholder: '请输入图片宽度', text: this.imageWidth.toFixed(0) })
  19.             .width(150)
  20.             .backgroundColor('#fff')
  21.             .type(InputType.Number)
  22.             .onChange(val=>{
  23.               console.log(val)
  24.               this.imageWidth=parseInt(val)
  25.             })
  26.         }
  27.         .width('100%')
  28.         .height(60)
  29.         .justifyContent(FlexAlign.SpaceAround)
  30.         Divider().width('91%')
  31.         Row(){
  32.           Button('缩小')
  33.             .width(80)
  34.             .height(30)
  35.             .type(ButtonType.Normal)
  36.             .onClick(()=>{
  37.               if(this.imageWidth>=10){
  38.                 this.imageWidth-=10
  39.               }
  40.             })
  41.           Button('放大')
  42.             .width(80)
  43.             .height(30)
  44.             .type(ButtonType.Normal)
  45.             .onClick(()=>{
  46.               if(this.imageWidth<=340){
  47.                 this.imageWidth+=10
  48.               }
  49.             })
  50.         }
  51.         .width('100%')
  52.         .height(60)
  53.         .justifyContent(FlexAlign.SpaceAround)
  54.         Slider({
  55.           min:10,
  56.           max:340,
  57.           value:this.imageWidth,
  58.           step:10,
  59.           style:SliderStyle.OutSet,
  60.           direction:Axis.Horizontal,
  61.           reverse:false
  62.         })
  63.           .width('90%')
  64.           .showTips(true)
  65.           .trackThickness(8)
  66.           .blockColor('#36d')
  67.           .onChange((val)=>{this.imageWidth=val})
  68.       }
  69.       .width('100%')
  70.       .height('100%')
  71.   }
  72. }
复制代码
 三、渲染控制(ForEach,if-else)

1.ForEach

a.语法

  1. ForEach(
  2.     //要遍历的数组数据
  3.     arr:Array,
  4.     //页面组件生成函数
  5.     (item:any,index?:number)=>{
  6.         Row(){
  7.             Image('mate60.jpg')
  8.             Column(){
  9.                 Text('mate60')
  10.                 Text('12700.00')
  11.             }
  12.         }
  13.     },
  14.     //键生成函数,为数组每一项生成一个唯一标识,组件是否重新渲染的判断标准
  15.     keyGenerator?:(item:any,index?:numbe):string=>{
  16.    
  17.     }
  18. )
复制代码
b.示例


  1. class Item {
  2.   name:string
  3.   image:ResourceStr
  4.   price:number
  5.   constructor( name:string, image:ResourceStr, price:number) {
  6.     this.name=name
  7.     this.image=image
  8.     this.price=price
  9.   }
  10. }
  11. @Entry
  12. @Component
  13. struct Index {
  14.   private items:Array<Item>=[
  15.     new Item('华为Mate60',$r('app.media.mate'),6999),
  16.     new Item('华为MateBookProX',$r('app.media.mate'),13999),
  17.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  18.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  19.     new Item('华为MateX5',$r('app.media.mate'),12999)
  20.   ]
  21.   build() {
  22.       Column({space:8}) {
  23.         Row(){
  24.           Text('商品列表')
  25.             .fontSize(20)
  26.             .fontWeight(FontWeight.Bold)
  27.         }
  28.         .width('100%')
  29.         .height(60)
  30.         .margin({bottom:20})
  31.         ForEach(
  32.           this.items,
  33.           (item:Item)=>{
  34.             Row({space:10}){
  35.               Image(item.image)
  36.                 .width('20%')
  37.               Column({space:4}){
  38.                 Text(item.name)
  39.                   .fontSize(20)
  40.                   .fontWeight(FontWeight.Bold)
  41.                 Text(`¥ ${item.price}`)
  42.                   .fontSize(20)
  43.                   .fontWeight(FontWeight.Bold)
  44.                   .fontColor('red')
  45.               }
  46.               .alignItems(HorizontalAlign.Start)
  47.             }
  48.             .width('92%')
  49.             .height(100)
  50.             .padding(10)
  51.             .backgroundColor('#fff')
  52.             .borderRadius(20)
  53.             .justifyContent(FlexAlign.Start)
  54.           }
  55.         )
  56.       }
  57.       .width('100%')
  58.       .height('100%')
  59.       .backgroundColor('#ccc')
  60.   }
  61. }
复制代码
 2.if-else条件渲染


  1. class Item {
  2.   name:string
  3.   image:ResourceStr
  4.   price:number
  5.   discount:number
  6.   constructor( name:string, image:ResourceStr, price:number,discount:number=0) {
  7.     this.name=name
  8.     this.image=image
  9.     this.price = price;
  10.     this.discount=discount
  11.   }
  12. }
  13. @Entry
  14. @Component
  15. struct Index {
  16.   private items:Array<Item>=[
  17.     new Item('华为Mate60',$r('app.media.mate'),6999,500),
  18.     new Item('华为MateBookProX',$r('app.media.mate'),13999),
  19.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  20.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  21.     new Item('华为MateX5',$r('app.media.mate'),12999)
  22.   ]
  23.   build() {
  24.       Column({space:8}) {
  25.         Row(){
  26.           Text('商品列表')
  27.             .fontSize(20)
  28.             .fontWeight(FontWeight.Bold)
  29.         }
  30.         .width('100%')
  31.         .height(60)
  32.         .margin({bottom:20})
  33.         ForEach(
  34.           this.items,
  35.           (item:Item)=>{
  36.             Row({space:10}){
  37.               Image(item.image)
  38.                 .width('20%')
  39.               Column({space:4}){
  40.                 if(item.discount){
  41.                   Text(item.name)
  42.                     .fontSize(20)
  43.                     .fontWeight(FontWeight.Bold)
  44.                   Text(`原件¥ ${item.price}`)
  45.                     .fontSize(16)
  46.                     .fontWeight(FontWeight.Bold)
  47.                     .fontColor('#ccc')
  48.                     .decoration({type:TextDecorationType.LineThrough})
  49.                   Text('折扣价¥ '+(item.price-item.discount))
  50.                     .fontSize(20)
  51.                     .fontWeight(FontWeight.Bold)
  52.                     .fontColor('red')
  53.                   Text(`补贴¥ ${item.discount}`)
  54.                     .fontSize(20)
  55.                     .fontWeight(FontWeight.Bold)
  56.                     .fontColor('red')
  57.                 }else{
  58.                   Text(item.name)
  59.                     .fontSize(20)
  60.                     .fontWeight(FontWeight.Bold)
  61.                   Text(`¥ ${item.price}`)
  62.                     .fontSize(20)
  63.                     .fontWeight(FontWeight.Bold)
  64.                     .fontColor('red')
  65.                 }
  66.               }
  67.               .alignItems(HorizontalAlign.Start)
  68.             }
  69.             .width('92%')
  70.             .height(120)
  71.             .padding(10)
  72.             .backgroundColor('#fff')
  73.             .borderRadius(20)
  74.             .justifyContent(FlexAlign.Start)
  75.           }
  76.         )
  77.       }
  78.       .width('100%')
  79.       .height('100%')
  80.       .backgroundColor('#ccc')
  81.   }
  82. }
复制代码
3.List

列表List是一种复杂容器,具备以下特点:
列表项ListItem数目过多超出屏幕后,会主动提供滚动功能
列表项ListItem既可以纵向分列,也可以横向分列
a.语法

  1. List({space:10}){
  2.     ForEach([1,2,3,4],(item)=>{
  3.         ListItem(){
  4.          //列表项内容,只能包含一个根组件
  5.         }
  6.     })
  7. }
  8. .width('100%')
  9. .listDirection(Axis.Vertical)//列表方向,默认纵向垂直,水平方向Horizontal
复制代码
b.示例


  1. class Item {
  2.   name:string
  3.   image:ResourceStr
  4.   price:number
  5.   discount:number
  6.   constructor( name:string, image:ResourceStr, price:number,discount:number=0) {
  7.     this.name=name
  8.     this.image=image
  9.     this.price = price;
  10.     this.discount=discount
  11.   }
  12. }
  13. @Entry
  14. @Component
  15. struct Index {
  16.   private items:Array<Item>=[
  17.     new Item('华为Mate60',$r('app.media.mate'),6999,500),
  18.     new Item('华为MateBookProX',$r('app.media.mate'),13999),
  19.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  20.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  21.     new Item('华为MateX5',$r('app.media.mate'),12999),
  22.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  23.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  24.     new Item('华为MateX5',$r('app.media.mate'),12999)
  25.   ]
  26.   build() {
  27.       Column({space:8}) {
  28.         Row(){
  29.           Text('商品列表')
  30.             .fontSize(20)
  31.             .fontWeight(FontWeight.Bold)
  32.         }
  33.         .width('100%')
  34.         .height(30)
  35.         .margin({bottom:20})
  36.         List({space:8}){
  37.           ForEach(this.items,(item:Item)=>{
  38.             ListItem(){
  39.               //列表项内容,只能包含一个根组件
  40.               Row({space:10}){
  41.                 Image(item.image)
  42.                   .width('20%')
  43.                 Column({space:4}){
  44.                   if(item.discount){
  45.                     Text(item.name)
  46.                       .fontSize(20)
  47.                       .fontWeight(FontWeight.Bold)
  48.                     Text(`原件¥ ${item.price}`)
  49.                       .fontSize(16)
  50.                       .fontWeight(FontWeight.Bold)
  51.                       .fontColor('#ccc')
  52.                       .decoration({type:TextDecorationType.LineThrough})
  53.                     Text('折扣价¥ '+(item.price-item.discount))
  54.                       .fontSize(20)
  55.                       .fontWeight(FontWeight.Bold)
  56.                       .fontColor('red')
  57.                     Text(`补贴¥ ${item.discount}`)
  58.                       .fontSize(20)
  59.                       .fontWeight(FontWeight.Bold)
  60.                       .fontColor('red')
  61.                   }else{
  62.                     Text(item.name)
  63.                       .fontSize(20)
  64.                       .fontWeight(FontWeight.Bold)
  65.                     Text(`¥ ${item.price}`)
  66.                       .fontSize(20)
  67.                       .fontWeight(FontWeight.Bold)
  68.                       .fontColor('red')
  69.                   }
  70.                 }
  71.                 .alignItems(HorizontalAlign.Start)
  72.               }
  73.               .width('92%')
  74.               .height(120)
  75.               .padding(10)
  76.               .backgroundColor('#fff')
  77.               .borderRadius(20)
  78.               .justifyContent(FlexAlign.Start)
  79.             }
  80.           })
  81.         }
  82.         .width('100%')
  83.         .layoutWeight(1)
  84.       }
  85.   }
  86. }
复制代码
四、自界说组件

1.拆分组件

  1. @Component
  2. struct Com {
  3.   private title:string=''
  4.   build(){
  5.     //组件UI描述
  6.   }
  7. }
  8. @Component
  9. struct XxxPage {
  10.   build(){
  11.     //使用组件
  12.     Com({title:'标题'})
  13.   }
  14. }
复制代码
2.在页面中引入Header组件

3.全局自界说构建函数

  1. @Builder function XxxBuilder(){
  2.     //UI描述
  3. }
  4. @Component
  5. struct XxxPage {
  6.     build(){
  7.         XxxBuilder()
  8.     }
  9. }
复制代码
4.局部自界说构建函数

  1. @Component
  2. struct XxxPage {
  3.     @Builder YyyBuilder(){
  4.         //UI描述
  5.     }
  6.     build(){
  7.         this.YyyBuilder()
  8.     }
  9. }
复制代码
5.全局公共样式函数

仅可封装组件的通用属性
  1. @Styles function fillScreen(){
  2.   .width('100%')
  3.   .height('100%')
  4.   .backgroundColor('#f6f6f6')
  5. }
  6. //Extend装饰器,继承模式,只能写在全局位置
  7. @Extend(Text) function priceText(){
  8.   .fontSize(20)
  9.   .fontWeight(FontWeight.Bold)
  10.   .fontColor('red')
  11. }
复制代码
6.局部公共样式函数

  1. @Component
  2. struct XxxPage {
  3.     @Builder YyyBuilder(){
  4.         //UI描述
  5.     }
  6.     build(){
  7.         this.YyyBuilder()
  8.         .fillScreen()
  9.     }
  10.     //局部公共样式函数
  11.     @Styles  fillScreen(){
  12.         .width('100%')
  13.         .height('100%')
  14.         .backgroundColor('#f6f6f6')
  15.     }
  16. }
复制代码
7.示例


  1. @Component
  2. export struct Header {
  3.   private title:ResourceStr|string=''
  4.   build(){
  5.     Row(){
  6.       Image($r('app.media.left'))
  7.         .width(30)
  8.         .onClick(()=>{})
  9.       Text(this.title)
  10.         .fontSize(20)
  11.         .fontWeight(FontWeight.Bold)
  12.       Blank()
  13.       Image($r('app.media.reset'))
  14.         .width(24)
  15.         .onClick(()=>{})
  16.     }
  17.     .width('100%')
  18.     .height(30)
  19.     .backgroundColor('#f6f6f6')
  20.   }
  21. }
复制代码
  1. import {Header} from '../Com/CommomCom';
  2. class Item {
  3.   name:string
  4.   image:ResourceStr
  5.   price:number
  6.   discount:number
  7.   constructor( name:string, image:ResourceStr, price:number,discount:number=0) {
  8.     this.name=name
  9.     this.image=image
  10.     this.price = price;
  11.     this.discount=discount
  12.   }
  13. }
  14. //全局自定义构建函数
  15. // @Builder function ItemCard(item:Item){
  16. // }
  17. //全局公共样式函数
  18. @Styles function fillScreen(){
  19.   .width('100%')
  20.   .height('100%')
  21.   .backgroundColor('#f6f6f6')
  22. }
  23. //继承模式,只能写在全局位置
  24. @Extend(Text) function priceText(){
  25.   .fontSize(20)
  26.   .fontWeight(FontWeight.Bold)
  27.   .fontColor('red')
  28. }
  29. @Entry
  30. @Component
  31. struct Index {
  32.   private items:Array<Item>=[
  33.     new Item('华为Mate60',$r('app.media.mate'),6999,500),
  34.     new Item('华为MateBookProX',$r('app.media.mate'),13999),
  35.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  36.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  37.     new Item('华为MateX5',$r('app.media.mate'),12999),
  38.     new Item('华为WatchGT4',$r('app.media.mate'),1438),
  39.     new Item('华为FreeBudPro3',$r('app.media.mate'),1499),
  40.     new Item('华为MateX5',$r('app.media.mate'),12999)
  41.   ]
  42.   build() {
  43.       Column({space:8}) {
  44.         //标题部分
  45.         Header({title:'商品列表'})
  46.           .margin({top:10})
  47.           .padding({left:20,right:20})
  48.         //商品列表部分
  49.         List({space:8}){
  50.           ForEach(this.items,(item:Item)=>{
  51.             ListItem(){
  52.               //列表项内容,只能包含一个根组件
  53.               this.ItemCard(item)
  54.             }
  55.           })
  56.         }
  57.         .width('100%')
  58.         .padding(20)
  59.         .layoutWeight(1)
  60.       }
  61.     .fillScreen()
  62.   }
  63.   //局部自定义构建函数
  64.   @Builder  ItemCard(item:Item){
  65.   Row({space:10}){
  66.     Image(item.image)
  67.       .width('20%')
  68.     Column({space:4}){
  69.       if(item.discount){
  70.         Text(item.name)
  71.           .fontSize(20)
  72.           .fontWeight(FontWeight.Bold)
  73.         Text(`原件¥ ${item.price}`)
  74.           .fontSize(16)
  75.           .fontWeight(FontWeight.Bold)
  76.           .fontColor('#ccc')
  77.           .decoration({type:TextDecorationType.LineThrough})
  78.         Text('折扣价¥ '+(item.price-item.discount))
  79.           .priceText()
  80.         Text(`补贴¥ ${item.discount}`)
  81.           .priceText()
  82.       }else{
  83.         Text(item.name)
  84.           .fontSize(20)
  85.           .fontWeight(FontWeight.Bold)
  86.         Text(`¥ ${item.price}`)
  87.           .priceText()
  88.       }
  89.     }
  90.     .alignItems(HorizontalAlign.Start)
  91.   }
  92.   .justifyContent(FlexAlign.Start)
  93.   .CardItemScreen()
  94.   }
  95.   //局部公共样式函数
  96.   @Styles  fillScreen(){
  97.     .width('100%')
  98.     .height('100%')
  99.     .backgroundColor('#f6f6f6')
  100.   }
  101.   @Styles  CardItemScreen(){
  102.     .width('100%')
  103.     .height(120)
  104.     .backgroundColor('#fff')
  105.     .borderRadius(20)
  106.   }
  107. }
复制代码


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

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

tsx81429

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