黑马视频HarmonyOS零底子

打印 上一主题 下一主题

主题 1009|帖子 1009|积分 3027

本文章是根据黑马步伐员鸿蒙 HarmonyOS NEXT星河版零底子入门到实战
00-鸿蒙开辟环境预备_哔哩哔哩_bilibili
https://www.bilibili.com/video/BV14t421W7pA?spm_id_from=333.788.videopod.episodes&vd_source=5d8817075e4d4e6bbfaa6c0251bdca76&p=2记录HarmonyOS的底子。
该视频真的非常得当有点了解前端的小同伴学习,里面有很多最底子的东西。
前端经验,react经验的小同伴看这个视频就非常的容易明白,直接看以下的条记,认真过一遍就可以了。
以下是我的训练仓库:
HarmonyOS-learn: 对HarmonyOS的底子学习记录 (gitee.com)
https://gitee.com/saro-d/harmonyOS-learn
   后续将连续更新该视频的条记
  ArkTs

  1. // 打印
  2. console.log('你好,我第一次来','黑马');
  3. // 变量
  4. let firstDay:string = '他们吵起来了'
  5. firstDay = '看视频吵起来的'
  6. // 常量
  7. const commoy:string ='这是个常量,不可修改'
  8. // 数组
  9. const names:string[] = ['小红','小明','笑话']
  10. // 函数
  11. function fn(val:number){
  12.   console.log('你好,我是函数', val)
  13. }
  14. fn(2)
  15. // 箭头函数
  16. const jianTouFn = (val:string)=>{
  17.   console.log('你好,我是箭头函数', val)
  18. }
  19. jianTouFn('箭头函数1')
  20. // 对象
  21. interface perObj{
  22.   name:string,
  23.   age:number,
  24.   statue:boolean,
  25.   sing:()=>void,
  26.   say:(val:string)=>string
  27. }
  28. const person:perObj = {
  29.   name:'大大',
  30.   age:18,
  31.   statue:false,
  32.   sing:() => {
  33.     console.log('唱歌了')
  34.   },
  35.   say:(val:string) => {
  36.     return val
  37.   }
  38. }
  39. person.sing()
  40. console.log('对象名',person.name)
  41. console.log('对象说话',person.say('你好'))
  42. // 联合类型
  43. let res:string | number |boolean = '优'
  44. res = 100
  45. res = false
  46. let gender:'man'|'woman'|'no' = 'no'  // 值已经被约束
  47. // 枚举类型
  48. enum ColorEnum{
  49.   Red='红色',
  50.   Black='黑色'
  51. }
  52. let sayColor:ColorEnum=ColorEnum.Red
  53. console.log('颜色',sayColor)
复制代码
ArkUI


组件:容器组件,底子组件


 根组件只有一个,以是最外层需要用Column容器组件包裹
  1. struct Index {
  2.   build() {
  3.     Column(){
  4.       Text('小说简介')
  5.       Row(){
  6.         Text('都市')
  7.         Text('言情')
  8.         Text('生活')
  9.         Text('男频')
  10.       }
  11.     }
  12.   }
  13. }
复制代码
组件字体属性方法


字体颜色


  1. struct Index {
  2.   build() {
  3.     Column(){
  4.       Text('小说简介')
  5.         .margin(10)
  6.         .fontSize(24)
  7.         .fontWeight(FontWeight.Bold)
  8.         .fontColor(Color.Pink)
  9.       Row(){
  10.         Text('都市')
  11.           .fontSize(20)
  12.           .fontColor(Color.Green)
  13.         Text('言情')
  14.           .margin(10)
  15.           .fontColor(Color.White)
  16.         Text('生活')
  17.           .fontSize(20)
  18.           .margin(10)
  19.           .fontColor(Color.Blue)
  20.         Text('男频')
  21.           .fontColor(Color.White)
  22.       }
  23.       .margin(10)
  24.     }
  25.     .width('100%')
  26.     .backgroundColor('#afb1b3')
  27.   }
  28. }
复制代码
笔墨溢出省略号,行高


  1.   Column(){
  2.         Text('鸿蒙开发初体验')
  3.           .width('100%')
  4.           .fontSize(24)
  5.           .fontWeight(FontWeight.Bold)
  6.         Text('文字溢出省略号,行高文字溢出省略号,行高文字溢出省略号,行高文字溢出省略号,行高文字溢出省略号,行高')
  7.           .width('100%')
  8.           .textOverflow({
  9.             overflow:TextOverflow.Ellipsis
  10.           })
  11.           .maxLines(2)
  12.           .lineHeight(30)
  13.       }
复制代码

 图片组件使用

  1. //   图片
  2.       Column(){
  3.         Row(){
  4.           Text('网络图片        ')
  5.           Image('https://www.itheima.com/images/logo.png')
  6.             .width(200)
  7.         }
  8.         Column(){
  9.           Image($r('app.media.Img01'))
  10.             .width(200)
  11.             .margin(20)
  12.           Row(){
  13.             Image($r('app.media.img02')).width(50) .margin(10)
  14.             Text('网络图片')
  15.           }
  16.         }
  17.         .margin(20)
  18.         .width('100%')
  19.       }
复制代码

输入框和按钮 


  1. @Entry
  2. @Component
  3. struct inputBtn {
  4.   build() {
  5.     Column({space:20}){
  6.       TextInput({
  7.         placeholder:'请输入用户名'
  8.       })
  9.       TextInput({
  10.         placeholder:'请输入密码'
  11.       })
  12.         .type(InputType.Password)
  13.       Button('登录')
  14.         .width(100)
  15.     }
  16.     .width(300)
  17.   }
  18. }
复制代码
登录页面训练 


  1. @Entry
  2. @Component
  3. struct Login {
  4.   build() {
  5.     Column({space:20}){
  6.       Image($r('app.media.img02')).width(100)
  7.       Column({space:20}){
  8.         TextInput({
  9.           placeholder:'请输入用户名'
  10.         })
  11.         TextInput({
  12.           placeholder:'请输入密码'
  13.         })
  14.           .type(InputType.Password)
  15.         Button('登录')
  16.           .width('100%')
  17.       }
  18.       Row({space:20}){
  19.         Text('前往注册')
  20.         Text('忘记密码')
  21.       }
  22.     }.padding(20)
  23.   }
  24. }
复制代码
Svg图标 


布局元素构成


内边距


外边距


边框


训练

  1. @Entry
  2. @Component
  3. struct QQLogin {
  4. @State user:string = '大王叫我来巡山'
  5.   build() {
  6.     Column(){
  7.       Image($r('app.media.img02')).width(100).borderRadius(50)
  8.       Text(this.user).margin({
  9.         top:20,
  10.         bottom:40
  11.       })
  12.       Column({space:15}){
  13.         Button('QQ登录')
  14.           .width('100%')
  15.         Button('微信登录')
  16.           .width('100%')
  17.           .backgroundColor('#dfdfdf')
  18.           .fontColor('#1e1e1e')
  19.       }
  20.       Row({space:20}){
  21.         Text('前往注册')
  22.         Text('忘记密码')
  23.       }.margin({
  24.         top:20
  25.       })
  26.     }.padding(20)
  27.   }
  28. }
复制代码
 

圆角

 特殊形状圆角




背景属性


背景图


背景图位置 


背景图单位

默认单位是px

背景图尺寸 


线性布局


主轴对齐


交织轴对齐


自适应伸缩



得物卡片


  1. @Entry
  2. @Component
  3. struct DeWuCard {
  4.   build() {
  5.     Column(){
  6.       Image($r('app.media.img02'))
  7.         .width('100%')
  8.         .height(250)
  9.         .borderRadius({
  10.           topLeft:10,
  11.           topRight:10
  12.         })
  13.       Text('今晚吃这个 | 每日艺术分享')
  14.         .width('100%')
  15.         .padding({
  16.           top:20,
  17.           right:10,
  18.           left:10,
  19.           bottom:20
  20.         })
  21.         .fontSize(20)
  22.         .fontWeight(FontWeight.Bold)
  23.       Row(){
  24.         Image($r('app.media.img02'))
  25.           .width(25)
  26.           .margin({
  27.             right:5
  28.           })
  29.         Text('插画师分享聚集地')
  30.           .layoutWeight(1)
  31.         Image($r('app.media.img02'))
  32.           .width(25)
  33.           .margin({
  34.             right:5
  35.           })
  36.         Text('2300')
  37.           .width(40)
  38.       }
  39.       .margin(10)
  40.     }
  41.     .width(300)
  42.     .height(360)
  43.     .backgroundColor(Color.Pink)
  44.     .borderRadius(10)
  45.   }
  46. }
复制代码
京东登录案例


  1. @Entry
  2. @Component
  3. struct JDLogin {
  4.   build() {
  5.     Column(){
  6.       Row(){
  7.         Image($r('app.media.cancel'))
  8.           .height(20)
  9.         Text('帮助')
  10.       }
  11.       .width('100%')
  12.       .height(30)
  13.       .justifyContent(FlexAlign.SpaceBetween)
  14.       Image($r('app.media.jdlogo'))
  15.         .width(200)
  16.         .margin({
  17.           top:50,
  18.           bottom:30
  19.         })
  20.     //   表单
  21.       Row(){
  22.         Text('国家/地址')
  23.           .fontColor('#666')
  24.           .height(16)
  25.           .layoutWeight(1)
  26.         Text('中国(+86)')
  27.           .fontColor('#666')
  28.           .height(16)
  29.         Image($r('app.media.ic_arrow_right'))
  30.           .fillColor('#666')
  31.           .height(16)
  32.       }
  33.       .backgroundColor('#fff')
  34.       .width('100%')
  35.       .padding(10)
  36.       .height(40)
  37.       .borderRadius(20)
  38.       Row(){
  39.         TextInput({
  40.           placeholder:'请输入手机号'
  41.         })
  42.           .placeholderColor('#666')
  43.           .height('100%')
  44.           .padding(0)
  45.           .backgroundColor('#fff')
  46.       }
  47.       .backgroundColor('#fff')
  48.       .width('100%')
  49.       .padding(10)
  50.       .height(40)
  51.       .borderRadius(20)
  52.       .margin({top:20,bottom:20})
  53.     //   条款
  54.       Row(){
  55.         Checkbox().width(10)
  56.         Text(){
  57.           Span('我已经阅读并同意')
  58.           Span('《京东隐私政策》《京东用户协议》')
  59.             .fontColor('#3274f6')
  60.           Span('未注册的手机号将自动创建京东账号')
  61.         }
  62.         .width('100%')
  63.         .fontSize(12)
  64.         .fontColor('#666')
  65.       }
  66.       .width('100%')
  67.       .margin({right:20,bottom:25})
  68.       .alignItems(VerticalAlign.Top)
  69.       Button('登录').width('100%').backgroundColor('#bf2838')
  70.       Row({space:25}){
  71.         Text('注册新用户')
  72.           .fontSize(14)
  73.         Text('账户密码登录')
  74.           .fontSize(14)
  75.         Text('无法登录')
  76.           .fontSize(14)
  77.       }
  78.       .margin({top:15})
  79.     //   其他
  80.       Blank()
  81.       Column(){
  82.         Text('其他登录方式')
  83.           .fontSize(14)
  84.           .height(22)
  85.           .fontColor('#666')
  86.         Row(){
  87.           Image($r('app.media.jd_huawei'))
  88.             .width(34)
  89.           Image($r('app.media.jd_wechat'))
  90.             .width(34)
  91.           Image($r('app.media.jd_weibo'))
  92.             .width(34)
  93.           Image($r('app.media.jd_QQ'))
  94.             .width(34)
  95.         }
  96.         .width('100%')
  97.         .padding(28)
  98.         .justifyContent(FlexAlign.SpaceBetween)
  99.       }
  100.       .height(100)
  101.     }
  102.     .padding(20)
  103.     .width('100%')
  104.     .height('100%')
  105.     .linearGradient({
  106.       angle:135,
  107.       direction:GradientDirection.RightBottom,
  108.       colors:[[0xb6d4d5,0.0],[0xebebeb,0.3],[0xf7e9e8,0.5],[0xffffff,1.0]]
  109.     })
  110.   }
  111. }
复制代码
弹性布局


例子 
  1. @Entry
  2. @Component
  3. struct Login {
  4.   build() {
  5.     Flex({
  6.       direction:FlexDirection.Column,
  7.       justifyContent:FlexAlign.Center,
  8.       alignItems:ItemAlign.Center
  9.     }){
  10.       Text().width(100).height(100).backgroundColor(Color.Pink).border({
  11.         width:1,
  12.         color:Color.Black })
  13.       Text().width(100).height(100).backgroundColor(Color.Pink).border({
  14.         width:1,
  15.         color:Color.Black })
  16.       Text().width(100).height(100).backgroundColor(Color.Pink).border({
  17.         width:1,
  18.         color:Color.Black })
  19.     }.width('100%').height(600).backgroundColor(Color.Green)
  20.   }
  21. }
复制代码
换行

Grid布局


例子:

间隙:


角标组件Badge



绝对定位和zIndex层级


  1. @Entry
  2. @Component
  3. struct JDLogin {
  4.   build() {
  5.     Column(){   
  6.     Text().width(100).height(100).backgroundColor(Color.Yellow).border({
  7.         width:1,
  8.         color:Color.Black })
  9.         .position({
  10.           x:50,
  11.           y:100
  12.         })
  13.         .zIndex(-1)
  14. }
  15. }
  16. }
复制代码
层叠布局




B站卡片案例


  1. @Entry
  2. @Component
  3. struct Login {
  4.   build() {
  5.     Column(){
  6.       Column(){
  7.         Stack({alignContent:Alignment.Bottom}){
  8.           Image($r('app.media.img02')).width('100%').height('100%')
  9.             .borderRadius({topLeft:20,topRight:20})
  10.           Row(){
  11.             Row({space:5}){
  12.               Image($r('app.media.jd_wechat')).width(16)
  13.               Text('288万').fontColor(Color.White)
  14.             }.margin({right:10})
  15.             Row({space:5}){
  16.               Image($r('app.media.jd_wechat')).width(16)
  17.               Text('288万').fontColor(Color.White)
  18.             }.layoutWeight(1)
  19.             // 除了写.layoutWeight(1),也可以写
  20.             // Blank()
  21.             Text('4:33').fontColor(Color.White)
  22.           }.width('100%').height(30).alignItems(VerticalAlign.Center).padding({right:5,left:5})
  23.         }.width(300).height(160)
  24.       //   文字
  25.         Text('【凤凰传奇新歌】欧迎来到国风统治区:唢呐一响神曲《铁衣【凤凰传奇新歌】欧迎来到国风统治区:唢呐一响神曲《铁衣')
  26.           .lineHeight(20).textOverflow({overflow:TextOverflow.Ellipsis}).maxLines(2)
  27.           .padding(10)
  28.         Row(){
  29.           Text('19万点赞').backgroundColor('#fff1f0').fontColor('#c77a60').padding(5)
  30.           Image($r('app.media.jd_wechat')).width(16).margin({right:5})
  31.         }.width('100%').justifyContent(FlexAlign.SpaceBetween).padding(10)
  32.       }.width(300).height(270).borderRadius(20).backgroundColor(Color.White)
  33.     }.width('100%').height('100%').backgroundColor('#f6f8fa')
  34.   }
  35. }
复制代码
阶段综合-付出宝首页


  1. @Entry
  2. @Component
  3. struct ZfbHome {
  4.   build() {
  5.     Stack({alignContent:Alignment.Bottom}){
  6.       Stack({alignContent:Alignment.Top}){
  7.         // 顶部搜索
  8.         Row(){
  9.           Column(){
  10.             Row(){
  11.               Text('重庆').fontColor(Color.White)
  12.               Image($r('app.media.top_down')).width(20).fillColor(Color.White)
  13.             }
  14.             Text('晴 2℃').fontColor(Color.White).fontSize(14)
  15.           }.height(33).alignItems(HorizontalAlign.Start)
  16.           Row(){
  17.             Image($r('app.media.top_sousuo')).width(20)
  18.             TextInput({
  19.               placeholder:'重庆交通一卡通'
  20.             }).placeholderColor('#666').layoutWeight(1)
  21.             Text('搜索').fontColor('#4c74ef').width(55).border({ width:{left:1},color:'#e9e9e9'}).textAlign(TextAlign.Center)
  22.           }.height(33).backgroundColor(Color.White).borderRadius(10)
  23.           .margin({left:12,right:12}).padding({left:10,right:10}).layoutWeight(1)
  24.           Image($r('app.media.top_add')).width(30).fillColor(Color.White)
  25.         }.width('100%').height(60).padding({left:10,right:10})
  26.         .backgroundColor('#6681ea').zIndex(2)
  27.         // 内容
  28.         Scroll(){
  29.           Column(){
  30.             Row(){
  31.               Column(){
  32.                 Image($r('app.media.head_sao')).width(28)
  33.                 Text('扫一扫').fontColor(Color.White)
  34.               }
  35.               Column(){
  36.                 Image($r('app.media.head_shoufukuan')).width(28)
  37.                 Text('收付款').fontColor(Color.White)
  38.               }
  39.               Column(){
  40.                 Image($r('app.media.head_chuxing')).width(28)
  41.                 Text('出行').fontColor(Color.White)
  42.               }
  43.               Column(){
  44.                 Image($r('app.media.head_car')).width(28)
  45.                 Text('卡包').fontColor(Color.White)
  46.               }
  47.             }.width('100%').margin({bottom:20})
  48.             .justifyContent(FlexAlign.SpaceAround).alignItems(VerticalAlign.Center)
  49.             Column(){
  50.               // 多功能
  51.               Flex({
  52.                 direction:FlexDirection.Row,
  53.                 justifyContent:FlexAlign.SpaceAround,
  54.                 alignContent:FlexAlign.SpaceAround,
  55.                 wrap:FlexWrap.Wrap
  56.               })
  57.               {
  58.                 Column(){
  59.                   Image($r('app.media.dd')).width(28)
  60.                   Text('滴滴')
  61.                 }.width('20%')
  62.                 Column(){
  63.                   Image($r('app.media.dd')).width(28)
  64.                   Text('滴滴')
  65.                 }.width('20%')
  66.                 Column(){
  67.                   Image($r('app.media.dd')).width(28)
  68.                   Text('滴滴')
  69.                 }.width('20%')
  70.                 Column(){
  71.                   Image($r('app.media.dd')).width(28)
  72.                   Text('滴滴')
  73.                 }.width('20%')
  74.                 Column(){
  75.                   Image($r('app.media.dd')).width(28)
  76.                   Text('滴滴')
  77.                 }.width('20%')
  78.                 Column(){
  79.                   Image($r('app.media.dd')).width(28)
  80.                   Text('滴滴')
  81.                 }.width('20%')
  82.                 Column(){
  83.                   Image($r('app.media.dd')).width(28)
  84.                   Text('滴滴')
  85.                 }.width('20%')
  86.                 Column(){
  87.                   Image($r('app.media.dd')).width(28)
  88.                   Text('滴滴')
  89.                 }.width('20%')
  90.                 Column(){
  91.                   Image($r('app.media.dd')).width(28)
  92.                   Text('滴滴')
  93.                 }.width('20%')
  94.                 Column(){
  95.                   Image($r('app.media.dd')).width(28)
  96.                   Text('滴滴')
  97.                 }.width('20%')
  98.                 Column(){
  99.                   Image($r('app.media.dd')).width(28)
  100.                   Text('滴滴')
  101.                 }.width('20%')
  102.                 Column(){
  103.                   Image($r('app.media.dd')).width(28)
  104.                   Text('滴滴')
  105.                 }.width('20%')
  106.                 Column(){
  107.                   Image($r('app.media.dd')).width(28)
  108.                   Text('滴滴')
  109.                 }.width('20%')
  110.                 Column(){
  111.                   Image($r('app.media.dd')).width(28)
  112.                   Text('滴滴')
  113.                 }.width('20%')
  114.                 Column(){
  115.                   Image($r('app.media.dd')).width(28)
  116.                   Text('滴滴')
  117.                 }.width('20%')
  118.               }.width('100%').height(170)
  119.               .backgroundColor(Color.White).borderRadius({topLeft:20,topRight:20})
  120.               //   推荐
  121.               Row(){
  122.                 Column(){
  123.                   Stack({alignContent:Alignment.Top}){
  124.                     Text().width('100%').height(40).backgroundColor('#fde5ff')
  125.                     Text('官方收钱码').padding(6).fontColor('#d6b5d9').fontSize(12).fontWeight(600)
  126.                       .backgroundColor('#FFF').borderRadius({bottomRight:10,bottomLeft:10})
  127.                   }
  128.                   Column(){
  129.                     Text('收钱码站出来').fontWeight(700).fontColor('#af6ac6').fontSize(15)
  130.                     Text('最高100元奖励').fontColor('#d6b5d9').fontSize(14)
  131.                     Image($r('app.media.shouju')).height(80).position({y:50,x:10})
  132.                   }.layoutWeight(1)
  133.                 }.height('100%').width(110).borderRadius(10).backgroundColor(Color.White)
  134.                 Column(){
  135.                   Row({space:2})
  136.                   {
  137.                     Image($r('app.media.jd_huawei')).width(20)
  138.                     Text('消费圈').fontWeight(600)
  139.                     Text('便宜').padding(4).linearGradient({
  140.                       angle:90,
  141.                       direction:GradientDirection.Right,
  142.                       colors:[[0xd79b5e,0.0],[0xda5e7a,1.0]]
  143.                     }).borderRadius(10).fontColor(Color.White).fontSize(11)
  144.                   }.width('100%').height(40).linearGradient({
  145.                     angle:90,
  146.                     direction:GradientDirection.Right,
  147.                     colors:[[0xfde3d2,0.0],[0xfdeee5,1.0]]
  148.                   })
  149.                   Image($r('app.media.img02')).width(80).margin({top:12,bottom:15})
  150.                   Row(){
  151.                     Image($r('app.media.jd_wechat')).width(30)
  152.                     Column(){
  153.                       Text('买绿色商品').fontSize(13).fontWeight(600)
  154.                       Text('得森林能量').fontSize(13).fontColor('#b5b5b5')
  155.                     }
  156.                   }
  157.                 }.height('100%').width(110).borderRadius(10).backgroundColor(Color.White)
  158.                 Column(){
  159.                   Row({space:2})
  160.                   {
  161.                     Image($r('app.media.jd_huawei')).width(20)
  162.                     Text('理好财').fontWeight(600)
  163.                     Text('精选').padding(4).linearGradient({
  164.                       angle:90,
  165.                       direction:GradientDirection.Right,
  166.                       colors:[[0xd79b5e,0.0],[0xda5e7a,1.0]]
  167.                     }).borderRadius(10).fontColor(Color.White).fontSize(11)
  168.                   }.width('100%').height(40).linearGradient({
  169.                     angle:90,
  170.                     direction:GradientDirection.Right,
  171.                     colors:[[0xd5e6fe,0.0],[0xe7efff,1.0]]
  172.                   })
  173.                   Image($r('app.media.img02')).width(80).margin({top:12,bottom:15})
  174.                   Row(){
  175.                     Image($r('app.media.jd_wechat')).width(30)
  176.                     Column(){
  177.                       Text('买绿色商品').fontSize(13).fontWeight(600)
  178.                       Text('得森林能量').fontSize(13).fontColor('#b5b5b5')
  179.                     }
  180.                   }
  181.                 }.height('100%').width(110).borderRadius(10).backgroundColor(Color.White)
  182.               }.width('100%').height(160).justifyContent(FlexAlign.SpaceAround).margin({top:10})
  183.               //   视频
  184.               Row(){
  185.                 Column(){
  186.                   Row({space:10}){
  187.                     Image($r('app.media.dd')).width(25)
  188.                     Text('市民中心·城市发布').fontWeight(700)
  189.                   }.width('100%').margin({bottom:10})
  190.                   Text('在重庆退休后能领多少养老金?').font({
  191.                     size:14,
  192.                     weight:600
  193.                   }).lineHeight(20)
  194.                   Row({space:20}){
  195.                     Text('怎样计算?').fontSize(14).fontColor('#c1c1c1')
  196.                     Text('政务办事').fontSize(14).padding(2).backgroundColor('#f9eccf').fontColor('#c88757')
  197.                   }.margin({bottom:20})
  198.                   Text('去看看').fontSize(16).fontWeight(600).padding(10)
  199.                     .backgroundColor('#f9eccf').fontColor('#c88757').borderRadius(10)
  200.                 }.layoutWeight(1).alignItems(HorizontalAlign.Start)
  201.                 Stack({
  202.                   alignContent:Alignment.TopStart
  203.                 })
  204.                 {
  205.                   Image($r('app.media.Img01')).borderRadius(10)
  206.                   Image($r('app.media.play')).width(30)
  207.                 }.width(130)
  208.               }.width('100%').height(160).padding(10).margin({top:20,bottom:20}).backgroundColor(Color.White)
  209.               //   消费圈
  210.               Flex(){}
  211.             }.width('100%').backgroundColor('#f8f8f8').borderRadius({topLeft:20,topRight:20})
  212.           }.width('100%').padding({top:60,bottom:60})
  213.           .backgroundColor('#6681ea')
  214.         }
  215.       }
  216.       // 底部Tab
  217.       Row(){
  218.         Column(){
  219.           Image($r('app.media.tab_zfb')).width(40)
  220.         }
  221.         Column(){
  222.           Image($r('app.media.tab_licai')).width(28)
  223.           Text('理财')
  224.         }
  225.         Column(){
  226.           Image($r('app.media.tab_life')).width(28)
  227.           Text('生活')
  228.         }
  229.         Column(){
  230.           Image($r('app.media.tab_xiaoxi')).width(28)
  231.           Text('消息')
  232.         }
  233.         Column(){
  234.           Image($r('app.media.tab_i')).width(28)
  235.           Text('我的')
  236.         }
  237.       }.width('100%').height(60).backgroundColor(Color.White)
  238.       .justifyContent(FlexAlign.SpaceAround).alignItems(VerticalAlign.Center)
  239.     }.width('100%').height('100%').backgroundColor(Color.Pink)
  240.   }
  241. }
复制代码

字符串拼接


模板字符串


范例转换(数字和字符串)

字符串转数字




数字转字符串


交互

点击事件

  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = 'Hello World';
  5.   build() {
  6.     Column(){
  7.       Button('点击喔')
  8.         .onClick(()=>{
  9.           AlertDialog.show({
  10.             message:'你好~这是个弹窗'
  11.           })
  12.         })
  13.     }
  14.   }
  15. }
复制代码
状态管理



  1. // 普通变量
  2. let msg:string='我是组件外普通变量'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   @State message: string = 'Hello World';
  7.   msg1:string='我是组件内普通变量'
  8.   msg2:string='我是组件内普通变量2'
  9.   build() {
  10.     Column(){
  11.       Text(msg)
  12.       Text(this.msg1)
  13.       Button(this.message)
  14.         .onClick(()=>{
  15.           AlertDialog.show({
  16.             message:'你好~这是个弹窗'
  17.           })
  18.           this.message = '已经点击了'
  19.         })
  20.     }
  21.   }
  22. }
复制代码
计算器案例

  1. @Entry
  2. @Component
  3. struct CounterPage{
  4. @State num:number = 0
  5.   build() {
  6.     Row({space:10}){
  7.       Button('-')
  8.         .onClick(()=>{
  9.           this.num--
  10.         })
  11.       Text(this.num.toString())
  12.       Button('+')
  13.         .onClick(()=>{
  14.           this.num++
  15.         })
  16.     }
  17.   }
  18. }
复制代码
运算符

算数运算符


赋值运算符


点赞案例


  1. @Entry
  2. @Component
  3. struct CounterPage{
  4.   @State count:number=999
  5.   @State supportColor:string =''
  6.   @State supportState:boolean = false
  7.   build() {
  8.     Column({space:10}){
  9.    
  10.     //   爱心
  11.       Row({space:10}){
  12.         Image($r('app.media.love')).width(30).fillColor(this.supportColor)
  13.         Text(this.count.toString()).fontColor(this.supportColor)
  14.       }
  15.       .onClick(()=>{
  16.         if(this.supportState){
  17.           this.supportColor = ''
  18.           this.count --
  19.           this.supportState = false
  20.         }else{
  21.           this.supportColor = '#8f1422'
  22.           this.count ++
  23.           this.supportState = true
  24.         }
  25.       })
  26.     }
  27.   }
  28. }
复制代码
一元运算符


比较运算符


逻辑运算符 

 

运算符优先级


美团购物车案例


  1. @Entry
  2. @Component
  3. struct ShoppingCart{
  4.   // 原价
  5.   @State priced:number=30
  6.   // 单价
  7.   @State unitPrice:number=20.2
  8.   // 数量
  9.   @State quantity:number=0
  10.   // 总价
  11.   @State allPrice:number=0
  12.   // 优惠
  13.   @State preferential:number=0
  14.   build() {
  15.     Stack({
  16.       alignContent:Alignment.Bottom
  17.     })
  18.     {
  19.       // 商品列表
  20.       Column(){
  21.         Row({space:5}){
  22.           Image($r('app.media.img02')).width(110).borderRadius(10)
  23.           Column({space:6}){
  24.             Text('冲销量1000ml缤纷八果水果捞').width('100%').font({size:14,weight:600})
  25.             Text('含1份折扣商品').width('100%').font({size:12}).fontColor('#8c8c8c')
  26.             Blank()
  27.             Row(){
  28.               Text(`¥${this.unitPrice}`).fontColor('#bd342a')
  29.               Text(`¥${this.priced}`).font({size:12}).fontColor('#8c8c8c').decoration({type:TextDecorationType.LineThrough}).margin({left:5})
  30.               Blank()
  31.               Row(){
  32.                 Text('-').width(20).height('100%').border({width:1,color:'#979797'}).textAlign(TextAlign.Center).fontWeight(600).borderRadius({topLeft:5,bottomLeft:5})
  33.                   .onClick(()=>{
  34.                     if(this.quantity == 0) return
  35.                     this.quantity --
  36.                     this.allPrice = this.quantity * this.unitPrice
  37.                     this.preferential = this.quantity * (this.priced - this.unitPrice)
  38.                   })
  39.                 Text(this.quantity.toString()).layoutWeight(1).height('100%').border({width:{top:1,bottom:1},color:'#979797'}).textAlign(TextAlign.Center).fontWeight(600)
  40.                 Text('+').width(20).height('100%').border({width:1,color:'#979797'}).textAlign(TextAlign.Center).fontWeight(600).borderRadius({topRight:5,bottomRight:5})
  41.                   .onClick(()=>{
  42.                     this.quantity ++
  43.                     this.allPrice = this.quantity * this.unitPrice
  44.                     this.preferential = this.quantity * (this.priced - this.unitPrice)
  45.                   })
  46.               }.width(75).height(25)
  47.             }.width('100%')
  48.           }.layoutWeight(1).height('100%')
  49.         }.height(83).width('100%')
  50.       }.width('100%').height('100%').padding(10)
  51.       // 结算
  52.       Row({space:10}){
  53.         Column(){
  54.             Text(){
  55.               Span(`已选 ${this.quantity} 件,`).fontColor('#919191')
  56.               Span('合计:')
  57.               Span(`¥${this.allPrice.toFixed(2)}`).fontColor('#bd342a')
  58.             }.fontSize(14)
  59.           Text(`共减¥${this.preferential.toFixed(2)}`).fontColor('#bd342a').fontSize(12)
  60.         }.layoutWeight(1).alignItems(HorizontalAlign.End)
  61.         Text('结算外卖').width(100).height(40).backgroundColor('#ffd441').font({size:17,weight:600}).textAlign(TextAlign.Center).borderRadius(20)
  62.       }.width('100%').height(100).backgroundColor(Color.White).padding(20)
  63.     }.backgroundColor('#f3f3f3').width('100%').height('100%')
  64.   }
  65. }
复制代码
数组操作




语句


if分支语句


if多分支


switch分支


三元条件表达式


条件渲染


条件渲染案例


  1. @Entry
  2. @Component
  3. struct JDAddCart {
  4.   // 库存状态
  5.   @State inventoryStatus:boolean = true
  6.   build() {
  7.     Column(){
  8.       if(this.inventoryStatus){
  9.         Text('还有一件商品')
  10.       }else {
  11.         Text('没有商品')
  12.       }
  13.       Row(){
  14.         Column(){
  15.           Image($r('app.media.love')).width(28)
  16.           Text('店铺')
  17.         }
  18.         Column(){
  19.           Image($r('app.media.love')).width(28)
  20.           Text('店铺')
  21.         }
  22.         Column(){
  23.           Image($r('app.media.love')).width(28)
  24.           Text('店铺')
  25.         }
  26.         if(this.inventoryStatus){
  27.           Text('加入购物车').width(100).height('100%').backgroundColor('#ffc73e').fontColor(Color.White).borderRadius(20).textAlign(TextAlign.Center)
  28.           Text('立即购买').width(100).height('100%').backgroundColor('#fa0028').fontColor(Color.White).borderRadius(20).textAlign(TextAlign.Center)
  29.         }else {
  30.           Text('查看类似商品').width(170).height('100%').backgroundColor('#ffc73e').fontColor(Color.White).borderRadius(20).textAlign(TextAlign.Center)
  31.         }
  32.       }.width('100%').height(60).justifyContent(FlexAlign.SpaceAround)
  33.       Button('切换').onClick(()=>{
  34.         this.inventoryStatus = !this.inventoryStatus
  35.       })
  36.     }
  37.   }
  38. }
复制代码
循环语句

while循环


 for循环


退出循环


遍历数组

for()


for...of



对象数组


ForEach-渲染控制



  1. interface  Article{
  2.   title:string,
  3.   createTime:string
  4. }
  5. @Entry
  6. @Component
  7. struct JDAddCart {
  8.   @State articles:Article[]=[
  9.     {
  10.       title:'近200+自动驾驶数据集全面调研!一览如何数据闭环全流程',
  11.       createTime:'2024-01-31 09:59:43'
  12.     },
  13.     {
  14.       title:'MySQL Shell 8.0.32 for GreatsQL编译二进制包',
  15.       createTime:'2024-01-31 09:55:53'
  16.     },
  17.     {
  18.       title:'在Redis中如何实现分布式事务的一致性?',
  19.       createTime:'2024-01-31 09:54:51'
  20.     },
  21.   ]
  22.   build() {
  23.     Column(){
  24.       Column({space:20}){
  25.         ForEach(this.articles,(item:Article,index)=>{
  26.           Column({space:10}){
  27.             Text(item.title).fontSize(16).fontWeight(600).width('100%')
  28.             Text(item.createTime).fontSize(12).fontColor('#cdcdcd').width('100%')
  29.           }.width('100%').border({width:{bottom:2},color:'#cdcdcd'}).padding(10)
  30.         })
  31.       }.width('100%')
  32.     }
  33.   }
  34. }
复制代码
生肖卡抽奖案例


  1. import { JSON } from '@kit.ArkTS'
  2. interface ZodiacCardItem{
  3.   url:string,
  4.   activeUrl:string,
  5.   count:number
  6. }
  7. // const t:number =  Math.floor(Math.random() *6)
  8. // console.log( '随机树:',t)
  9. @Entry
  10. @Component
  11. struct ZodiacDraw {
  12. //   卡片数据
  13.   @State zodiacCards:ZodiacCardItem[] = [
  14.     { url:'app.media.dog',activeUrl:'app.media.active_dog',count:1},
  15.     { url:'app.media.hu',activeUrl:'app.media.active_hu',count:1},
  16.     { url:'app.media.long',activeUrl:'app.media.active_long',count:1},
  17.     { url:'app.media.ma',activeUrl:'app.media.active_ma',count:0},
  18.     { url:'app.media.niu',activeUrl:'app.media.active_niu',count:1},
  19.     { url:'app.media.tu',activeUrl:'app.media.active_tu',count:1}
  20.   ]
  21.   // 页面层次
  22.   @State takePagezIndex:number = -1
  23.   @State takePageOpacity:number = 0
  24.   @State phoneStatus:boolean = true
  25.   // 抽中下标
  26.   @State drawnIndex:number = -1
  27.   // 抽中动画
  28.   @State drawnScale:number = 0
  29.   // 中奖奖池
  30.   @State prizePools:string[] = ['phone','huawei','xiaomi']
  31.   @State prize:string = ''
  32.   build() {
  33.     Stack(){
  34.       // 抽奖页
  35.       Column(){
  36.         Grid(){
  37.           ForEach(this.zodiacCards,(item:ZodiacCardItem,index)=>{
  38.             GridItem(){
  39.               Badge({
  40.                 count:item.count,
  41.                 position:BadgePosition.RightTop,
  42.                 style:{
  43.                   fontSize:14,
  44.                   badgeSize:20,
  45.                   badgeColor:'#fa2a2d'
  46.                 }
  47.               })
  48.               {
  49.                 // 如果count不为0,就渲染激活图片
  50.                 Image($r(`${item.count == 0 ? item.url : item.activeUrl}`))
  51.                   .width(90).border({width:{left:1,bottom:1},color:'#ffd4d4d4'})
  52.               }
  53.             }
  54.           })
  55.         }
  56.         .width('100%').height(300)
  57.         .margin({top:60,bottom:50})
  58.         .columnsTemplate('1fr 1fr 1fr').rowsTemplate('1fr 1fr')
  59.         Button('立即抽卡')
  60.           .backgroundColor('#ed4481').width(300)
  61.           .onClick(()=>{
  62.             // 结果页出
  63.             this.takePagezIndex = 1
  64.             this.takePageOpacity = 1
  65.             // 动画
  66.             this.drawnScale =1
  67.             // 结果卡片 随机
  68.             this.drawnIndex = Math.floor(Math.random() *6)
  69.           })
  70.       }
  71.       // 抽奖结果页
  72.       Column({space:25}){
  73.         Text('获得生肖卡').fontColor('#fff').fontSize(20)
  74.         Image($r(`${this.drawnIndex !==-1?this.zodiacCards[this.drawnIndex].activeUrl:null}`))
  75.           .width(140).borderRadius(20)
  76.           .scale({x:this.drawnScale,y:this.drawnScale}).animation({duration:500})
  77.         Button('开心收下')
  78.           .width(140).backgroundColor('#00000000').border({width:1,color:'#fff'})
  79.           .onClick(()=>{
  80.             // 结果页出
  81.             this.takePagezIndex = -1
  82.             this.takePageOpacity = 0
  83.             //   结果卡片
  84.             // 处理数组数据,对象数组的情况需要更新,就得修改整个对象
  85.             this.zodiacCards[this.drawnIndex] = {
  86.               url:this.zodiacCards[this.drawnIndex].url,
  87.               activeUrl:this.zodiacCards[this.drawnIndex].activeUrl,
  88.               count:this.zodiacCards[this.drawnIndex].count + 1
  89.             }
  90.           //   判断是否集齐卡片
  91.            this.phoneStatus = this.zodiacCards.some((item)=>item.count == 0)
  92.           //   判断方法2
  93.           //   let flag:boolean=true
  94.           //   for (let item of this.zodiacCards){
  95.           //     if(item.count ==0){
  96.           //       flag = false
  97.           //       break
  98.           //     }
  99.           //   }
  100.           //   如果集齐
  101.             if(!this.phoneStatus){
  102.               const randomIndex = Math.floor(Math.random() * 3)
  103.               this.prize = this.prizePools[randomIndex]
  104.             }
  105.           })
  106.       }
  107.       .width('100%').height('100%')
  108.       .backgroundColor('#bc000000')
  109.       .justifyContent(FlexAlign.Center)
  110.       .zIndex(this.takePagezIndex)
  111.       .opacity(this.takePageOpacity)
  112.       .animation({duration:300})
  113.     //   手机页
  114.       if(!this.phoneStatus){
  115.         Column({space:25}){
  116.           Text('恭喜获得手机一部').fontColor('#fff').fontSize(20)
  117.           Image($r(`app.media.${this.prize}`))
  118.             .width(300).borderRadius(20)
  119.             .scale({x:this.drawnScale,y:this.drawnScale}).animation({duration:500})
  120.           Button('再来一次')
  121.             .width(140).backgroundColor('#00000000').border({width:1,color:'#fff'})
  122.             .onClick(()=>{
  123.             //   将之前卡片所有数量-1
  124.             //   let arr:ZodiacCardItem[] =[]
  125.             //   this.zodiacCards.forEach((item:ZodiacCardItem)=>{
  126.             //     let obj:ZodiacCardItem = {
  127.             //       url: item.url,
  128.             //       activeUrl: item.activeUrl,
  129.             //       count:  item.count - 1
  130.             //     }
  131.             //     arr.push(obj)
  132.             //   })
  133.             //   this.zodiacCards= arr
  134.             //   方法2
  135.               this.zodiacCards = this.zodiacCards.map((item: ZodiacCardItem) => ({
  136.                 url: item.url,
  137.                 activeUrl: item.activeUrl,
  138.                 count:  item.count - 1
  139.               }) as ZodiacCardItem)
  140.               this.phoneStatus = true
  141.             })
  142.         }
  143.         .width('100%').height('100%')
  144.         .backgroundColor('#bc000000')
  145.         .justifyContent(FlexAlign.Center)
  146.         .animation({duration:300})
  147.       }
  148.     }
  149.   }
  150. }
复制代码
swiper轮播组件

基本用法

需要给swiper设置尺寸,若不设置则是内容自动撑开,也可以给内容每个单独设置。
  1. @Entry
  2. @Component
  3. struct SwiperPage {
  4.   build() {
  5.     Column(){
  6.       Swiper(){
  7.         Text('你好').backgroundColor(Color.Blue)
  8.         Text('我是').backgroundColor(Color.Gray)
  9.         Text('大大').backgroundColor(Color.Green)
  10.       }.width('100%').height(150)
  11.       Swiper(){
  12.         Image($r('app.media.active_niu'))
  13.         Image($r('app.media.active_ma'))
  14.         Image($r('app.media.active_long'))
  15.         Image($r('app.media.active_hu'))
  16.       }.width(150).height(200).margin({top:20}).border({width:1})
  17.     }
  18.   }
  19. }
复制代码
常见属性


  1. @Entry
  2. @Component
  3. struct SwiperPage {
  4.   build() {
  5.     Column(){
  6.       Swiper(){
  7.         Text('你好').backgroundColor(Color.Blue)
  8.         Text('我是').backgroundColor(Color.Gray)
  9.         Text('大大').backgroundColor(Color.Green)
  10.       }.width('100%').height(150)
  11.       .loop(true)
  12.       .autoPlay(true)
  13.       .interval(2000)
  14.       Swiper(){
  15.         Image($r('app.media.active_niu'))
  16.         Image($r('app.media.active_ma'))
  17.         Image($r('app.media.active_long'))
  18.         Image($r('app.media.active_hu'))
  19.       }.width('100%').height(400).margin({top:20}).border({width:1})
  20.       .loop(true)
  21.       .autoPlay(true)
  22.       .interval(3000)
  23.       .vertical(true)
  24.     }
  25.   }
  26. }
复制代码
样式自定义

aspectRatio()宽高比属性,设置和图片一致的宽高比,包管图片正常适配


代码演示
  1. @Entry
  2. @Component
  3. struct SwiperPage {
  4.   build() {
  5.     Column(){
  6.       Swiper(){
  7.         Image($r('app.media.active_niu'))
  8.         Image($r('app.media.active_ma'))
  9.         Image($r('app.media.active_long'))
  10.         Image($r('app.media.active_hu'))
  11.       }.width('100%').aspectRatio(0.85).margin({top:20}).border({width:1})
  12.       .loop(true)
  13.       .autoPlay(true)
  14.       .interval(4000)
  15.       .indicator(
  16.         Indicator.dot()
  17.           .itemWidth(10)
  18.           .selectedItemWidth(30)
  19.           .selectedColor(Color.Green)
  20.       )
  21.     }
  22.   }
  23. }
复制代码
样式&结构的重用

@Extend:扩展组件(样式,事件)【偏重组件】

@Extend(扩展组件) Extend的参数是需要扩展的组件如Text,Column等
实用特定组件 样式、事件
  1. @Extend(Text)
  2. function txtFn (){
  3.   .fontSize(20)
  4.   .fontWeight(FontWeight.Bolder)
  5.   .margin({bottom:20})
  6. }
  7. @Extend(Text)
  8. function swiperTxtFn (bgColor:ResourceColor,msg:string) {
  9.   .textAlign(TextAlign.Center)
  10.   .fontSize(20)
  11.   .fontWeight(FontWeight.Bolder)
  12.   .fontColor(Color.White)
  13.   .backgroundColor(bgColor)
  14.   .onClick(()=>{
  15.     AlertDialog.show({
  16.       message:msg
  17.     })
  18.   })
  19. }
  20. @Entry
  21. @Component
  22. struct ExtendPage {
  23.   build() {
  24.     Column(){
  25.       Text('@Extend:扩展组件(样式,事件)').txtFn()
  26.       Swiper(){
  27.         Text('你好').swiperTxtFn(Color.Gray,'你好轮播')
  28.         Text('我是').swiperTxtFn(Color.Green,'我是轮播')
  29.         Text('大大').swiperTxtFn(Color.Red,'轮播大大')
  30.       }.width('100%').height(150)
  31.       .loop(true)
  32.       .autoPlay(true)
  33.       .interval(2000)
  34.     }
  35.   }
  36. }
复制代码
@Styles:抽取通用属性,事件【偏重样式】

@Styles不支持传参,最好写组件内
实用公共样式、事件,不可以传参
  1. // 全局定义style
  2. @Styles function  commonStyles (){
  3.   .width(100)
  4.   .height(100)
  5. }
  6. @Entry
  7. @Component
  8. struct StylePage {
  9.   @State bgColor:ResourceColor = Color.Gray
  10.   // 组件内定义style
  11.   @Styles setBg (){
  12.     .backgroundColor(this.bgColor)
  13.     .onClick(()=>{
  14.       this.bgColor = Color.Green
  15.     })
  16. }
  17.   build() {
  18.     Column({space:20}){
  19.       Text('@Styles').commonStyles().setBg().fontColor(Color.White)
  20.       Text().commonStyles().setBg()
  21.       Text().commonStyles().borderRadius(50).setBg()
  22.     }.width('100%').justifyContent(FlexAlign.Center)
  23.   }
  24. }
复制代码
@Builder:自定义构建函数(结构,样式,事件)【偏重结构】

全局定义的函数在使用时不消写this,组件内定义的在使用时要写this
  1. // 全局定义
  2. @Builder
  3. function navItem (icon:ResourceStr,txt:string){
  4.   Column({space:10}){
  5.     Image(icon).width(70).height(70)
  6.     Text(txt)
  7.   }.onClick(()=>{
  8.     AlertDialog.show({
  9.       message:'这里是' + txt
  10.     })
  11.   })
  12. }
  13. @Entry
  14. @Component
  15. struct BuilderPage {
  16.   // 组件内定义
  17.   @Builder
  18.   navItem (icon:ResourceStr,txt:string){
  19.     Column({space:10}){
  20.       Image(icon).width(70).height(70)
  21.       Text(txt)
  22.     }.onClick(()=>{
  23.       AlertDialog.show({
  24.         message:'这里是' + txt + '  @Builder'
  25.       })
  26.     })
  27. }
  28.   build() {
  29.     Row({space:20})
  30.     {
  31.       navItem($r('app.media.yaodiao'),'阿里药店')
  32.       navItem($r('app.media.paimai'),'阿里拍卖')
  33.       this.navItem($r('app.media.nongchang'),'阿里农场')
  34.       this.navItem($r('app.media.yizhan'),'菜鸟驿站')
  35.     }.width('100%').justifyContent(FlexAlign.Center)
  36.   }
  37. }
复制代码
滚动容器Scroll

核心用法


  1. @Entry
  2. @Component
  3. struct ScrollPage {
  4.   build() {
  5.     Scroll(){
  6.       Column({space:10}){
  7.         ForEach(Array.from({length:10}),(item:string,index)=>{
  8.           Text(`测试文本${index + 1}`)
  9.             .width('100%').height(50)
  10.             .fontColor(Color.White)
  11.             .backgroundColor(Color.Orange)
  12.             .textAlign(TextAlign.Center)
  13.         })
  14.       }.width('100%').padding(10)
  15.     }.width('100%').height(400)
  16.   }
  17. }
复制代码
常见属性


  1. @Entry
  2. @Component
  3. struct ScrollPage {
  4.   build() {
  5.     Scroll(){
  6.       Column({space:10}){
  7.         ForEach(Array.from({length:10}),(item:string,index)=>{
  8.           Text(`测试文本${index + 1}`)
  9.             .width('100%').height(50)
  10.             .fontColor(Color.White)
  11.             .backgroundColor(Color.Orange)
  12.             .textAlign(TextAlign.Center)
  13.         })
  14.       }
  15.       .width('100%').padding(10)
  16.     }
  17.     .width('100%').height(400)
  18.     .scrollable(ScrollDirection.Vertical) // 滚动方向
  19.     .scrollBar(BarState.Auto) // On 一直显示,Off一直隐藏 Auto滑动显示
  20.     .scrollBarWidth(5) // 滚动条宽度
  21.     .edgeEffect(EdgeEffect.Spring) // 滑动效果
  22.     .scrollBarColor(Color.Green)
  23.   }
  24. }
复制代码
控制器


  1. @Entry
  2. @Component
  3. struct ScrollPage {
  4.   // 1.创建Scroll 对象(实例化)
  5.   myScroll:Scroller = new Scroller()
  6.   build() {
  7.     Column(){
  8.       // 2.绑定给Scroll组件
  9.       Scroll(this.myScroll){
  10.         Column({space:10}){
  11.           ForEach(Array.from({length:10}),(item:string,index)=>{
  12.             Text(`测试文本${index + 1}`)
  13.               .width('100%').height(50)
  14.               .fontColor(Color.White)
  15.               .backgroundColor(Color.Orange)
  16.               .textAlign(TextAlign.Center)
  17.           })
  18.         }
  19.         .width('100%').padding(10)
  20.       }
  21.       .width('100%').height(400).margin({bottom:50})
  22.       .scrollable(ScrollDirection.Vertical) // 滚动方向
  23.       .scrollBar(BarState.Auto) // On 一直显示,Off一直隐藏 Auto滑动显示
  24.       .scrollBarWidth(5) // 滚动条宽度
  25.       .edgeEffect(EdgeEffect.Spring) // 滑动效果
  26.       .scrollBarColor(Color.Green)
  27.       Button('回到顶部/底部')
  28.         .onClick(()=>{
  29.           this.myScroll.scrollEdge(Edge.Bottom)  // Edge.Top Edge.Start回到顶部
  30.         })
  31.       Button('获取滚动位置').margin(20)
  32.         .onClick(()=>{
  33.           const y = this.myScroll.currentOffset().yOffset
  34.           AlertDialog.show({
  35.             message:`y:${y}`
  36.           })
  37.         })
  38.     }
  39.   }
  40. }
复制代码
事件


返回顶部案例


  1. @Entry
  2. @Component
  3. struct ScrollPage {
  4.   // 1.创建Scroll 对象(实例化)
  5.   myScroll:Scroller = new Scroller()
  6.   // 距离顶部位置
  7.   @State scrollTop:number = 0
  8.   build() {
  9.   //   淘宝滚动案例
  10.     Column(){
  11.       Scroll(this.myScroll){
  12.         Column(){
  13.           Image($r('app.media.t_b'))
  14.         }
  15.       }.width('100%')
  16.       .height('100%')
  17.       .onScroll((x,y)=>{
  18.         this.scrollTop = this.myScroll.currentOffset().yOffset
  19.         // console.log('x,y',x,y,this.scrollTop)
  20.       })
  21.       if( this.scrollTop >= 200){
  22.         Row(){
  23.           Image($r("app.media.rocket")) .width(50)
  24.         }
  25.         .position({bottom:30,right:20})
  26.         .backgroundColor(Color.White)
  27.         .borderRadius(25)
  28.         .onClick(()=>{
  29.           this.myScroll.scrollEdge(Edge.Top)
  30.         })
  31.       }
  32.     }
  33.   }
  34. }
复制代码
列表组件list




字体图标



容器组件Tabs

基本用法


  1. @Entry
  2. @Component
  3. struct TabsPage {
  4.   build() {
  5.     Tabs(){
  6.       TabContent(){ // 有且只能有一个子组件
  7.         Text('首页内容')
  8.       }
  9.       .tabBar('首页') // 配置导航  也可以不配
  10.       TabContent(){ // 有且只能有一个子组件
  11.         Text('推荐内容')
  12.       }
  13.       .tabBar('推荐')
  14.       TabContent(){ // 有且只能有一个子组件
  15.         Text('我的内容')
  16.       }
  17.       .tabBar('我的')
  18.       
  19.     }
  20.   }
  21. }
复制代码
常用属性


滚动导航栏


  1. @Entry
  2. @Component
  3. struct TabsPage {
  4.   @State titles:string[] = [
  5.     '首页','关注','热门','军事','体育',
  6.   '八卦','数码','财经','美食','旅行'
  7.   ]
  8.   build() {
  9.     Tabs(){
  10.       ForEach(this.titles,(item:string,index)=>{
  11.           TabContent(){ // 有且只能有一个子组件
  12.             Text(`${item}内容`)
  13.           }
  14.           .tabBar(item) // 配置导航  也可以不配
  15.       })
  16.     }
  17.     .barMode(BarMode.Scrollable)
  18.   }
  19. }
复制代码
自定义TaBar


切换高亮


  1. interface Item{
  2.   title:string,
  3.   icon:ResourceStr
  4. }
  5. @Entry
  6. @Component
  7. struct TabsPage {
  8.   @State tabList:Item[] = [
  9.     {
  10.       title:'首页',
  11.       icon:$r('app.media.home')
  12.     },
  13.     {
  14.       title:'推荐',
  15.       icon: $r('app.media.tuijian')
  16.     },
  17.     {
  18.       title:'新增',
  19.       icon:$r('app.media.add')
  20.     },
  21.     {
  22.       title:'我的',
  23.       icon:$r('app.media.my')
  24.     },
  25.   ]
  26.   // 选择tab
  27.   @State selectTabIndex:number = 0
  28.   // 构建tab
  29.   @Builder
  30.   myTabBuilder(itemIndex:number,icon:ResourceStr,title:string){
  31.     Column({space:8}){
  32.       Image(icon).width(30).fillColor(`${this.selectTabIndex == itemIndex ? Color.Orange:Color.Black}`)
  33.       Text(title).fontColor(`${this.selectTabIndex == itemIndex ? Color.Orange:Color.Black}`)
  34.     }
  35.   }
  36.   build() {
  37.     // 切换高亮
  38.     Tabs({barPosition:BarPosition.End}){
  39.       ForEach(this.tabList,(item:Item,index)=>{
  40.           TabContent(){ // 有且只能有一个子组件
  41.             Text(`${item.title}内容`)
  42.           }
  43.           .tabBar(this.myTabBuilder(index,item.icon,item.title)) // 配置导航  也可以不配
  44.       })
  45.     }.onChange((index)=>{
  46.       this.selectTabIndex = index
  47.     })
  48.   }
  49. }
复制代码
案例:小米有品底部Tabs


  1. interface Item{
  2.   title:string,
  3.   icon:ResourceStr
  4. }
  5. @Entry
  6. @Component
  7. struct TabsPage {
  8.   @State tabList:Item[] = [
  9.     {
  10.       title:'首页',
  11.       icon:$r('app.media.home')
  12.     },
  13.     {
  14.       title:'推荐',
  15.       icon: $r('app.media.tuijian')
  16.     },
  17.     {
  18.       title:'推买',
  19.       icon: $r('app.media.yizhan')
  20.     },
  21.     {
  22.       title:'新增',
  23.       icon:$r('app.media.add')
  24.     },
  25.     {
  26.       title:'我的',
  27.       icon:$r('app.media.my')
  28.     },
  29.   ]
  30.   // 选择tab
  31.   @State selectTabIndex:number = 0
  32.   // 构建tab
  33.   @Builder
  34.   myTabBuilder(itemIndex:number,icon:ResourceStr,title:string){
  35.     Column({space:8}){
  36.       Image(icon).width(28).fillColor(`${this.selectTabIndex == itemIndex ? Color.Orange:Color.Black}`)
  37.       Text(title).fontColor(`${this.selectTabIndex == itemIndex ? Color.Orange:Color.Black}`)
  38.     }
  39.   }
  40.   // 构建中间的tab
  41.   @Builder
  42.   centerTabBuilder(icon:ResourceStr){
  43.     Image(icon).width(50)
  44.   }
  45.   build() {
  46.     // 切换高亮
  47.     Tabs({barPosition:BarPosition.End}){
  48.       ForEach(this.tabList,(item:Item,index)=>{
  49.         if(index == 2){
  50.           TabContent(){ // 有且只能有一个子组件
  51.             Text(`${item.title}内容`)
  52.           }
  53.           .tabBar(this.centerTabBuilder(item.icon))
  54.         }else {
  55.           TabContent(){ // 有且只能有一个子组件
  56.             Text(`${item.title}内容`)
  57.           }
  58.           .tabBar(this.myTabBuilder(index,item.icon,item.title)) // 配置导航  也可以不配
  59.         }
  60.       })
  61.     }.onChange((index)=>{
  62.       this.selectTabIndex = index
  63.     })
  64.   }
  65. }
复制代码
Class类

Class类 实例属性



  1. // 实例对象
  2. class Cat{
  3.   name:string = '小十'
  4.   food?:string
  5. }
  6. let catObj = new Cat()
  7. catObj.food = '鸡肉猫条'
  8. console.log('名字:',catObj.name)
  9. console.log('食物:',catObj.food)
  10. @Entry
  11. @Component
  12. struct classPage {
  13.   build(){}
  14. }
复制代码

Class类 构造函数


差别的实例,未来需要有差别的字段初始值,就需要通过构造函数实现


[code]// ---------------------构造函数---------------------
interface  CatObj{
  food:string
  age:number
}
class Cat{
  name:string = '小十'
  food?:string
  age?:number

  constructor(paramsObj:CatObj) {
    // this.name = name
    this.food = paramsObj.food
    this.age = paramsObj.age
  }
}
let catDay1 = new Cat({food:'鸡肉猫条',age:90})
console.log(`第 ${catDay1.age} 天吃{catDay1.food}`)
let catDay2 = new Cat({food:'金枪

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

西河刘卡车医

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