ArkTS语法

一、变量

声明变量的形式:let 变量名:变量类型 = 值

例如:

二、数组

 声明数组的形式:let 数组名:变量类型[ ] = [ 元素1,元素2,元素3,......]

三、对象

在定义对象之前要先定义接口,用来约定对象的数据类型

 声明对象的形式:

let 对象名:对象类型 = {

        变量1:值,

        变量2:值

}

四、函数

声明函数的形式:

function 函数名 (参数1:数据类型,参数2:数据类型) {

        return 返回值

}

五、箭头函数

声明箭头函数的形式:

let 函数名 =(参数1:数据类型,参数2:数据类型)=> {

        return 返回值

}

六、组件

界面中的文字、图片、按钮都是组件

先写容器组件,再写内容组件

容器组件:Column() {}, Row() {}

内容组件:Text(),无{}

七、组件属性 

通用属性:

文本属性:

 八、图像组件

九、内外边距

十、边框属性

十一、综合案例——歌曲列表

 

代码:

@Entry
@Component
struct Index {

  build() {
    Column() {
      Text('猜你喜欢')
        .fontColor(Color.White)
        .width('100%')
        .margin({
          bottom: 10
        })
      List() {
        ListItem() {
          Row() {
            // 图片
            Image($r('app.media.qiu'))
              .width(80)
              .margin({
                right: 10
              })
              .border({
                radius: 8
              })
            // 歌曲信息
            Column() {
              Text('直到世界尽头')
                .fontColor(Color.White)
                .fontWeight(700)
                .width('100%')
                .margin({
                  bottom: 15
                })
              // VIP及作者
              Row() {
                Text('VIP')
                  .border({
                    width: 1,
                    color: Color.Orange,
                    radius: 12
                  })
                  .padding({
                    top: 3,
                    bottom: 3,
                    left: 5,
                    right: 5
                  })
                  .margin({
                    right: 10
                  })
                  .fontColor(Color.White)
                Text('WANDS')
                  .fontColor(Color.Gray)
              }
              .width('100%')
            }
            .layoutWeight(1)
            // 更多
            Image($r("app.media.ic_more"))
              .height(30)
              .fillColor(Color.White)
          }
            .width('100%')
            .height(80)
            .padding({
              left: 10,
              right: 10
            })
            .margin({
              bottom: 10
            })
          //.backgroundColor(Color.Pink)
        }
      }
        .scrollBar(BarState.Off)
    }
      .width('100%')
      .height('100%')
      .padding({
        left: 10,
        right: 10
      })
      .backgroundColor(Color.Black)
      .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
  }
}

十二、if分支、条件表达式、循环

if-else分支

条件表达式

循环渲染

十三、状态管理和事件 

十四、自定义构建函数

十五、综合案例:循环渲染歌曲列表、增加播放交互

效果:

代码:

interface SongItemType {
  img: string //图像
  name: string //歌名
  author: string //作者
}

@Entry
@ComponentV2
struct Index {
  songs: SongItemType[] = [
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/0.jpg',
      name: '直到世界的尽头',
      author: 'WANDS',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/1.jpg',
      name: '画',
      author: '赵磊',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/2.jpg',
      name: 'Sweet Dreams',
      author: 'TPaul Sax / Eurythmics',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/3.jpg',
      name: '奢香夫人',
      author: '凤凰传奇',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/4.jpg',
      name: '空心',
      author: '光泽',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/5.jpg',
      name: '反转地球',
      author: '潘玮柏',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/6.jpg',
      name: 'No.9',
      author: 'T-ara',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/7.jpg',
      name: '孤独',
      author: 'G.E.M.邓紫棋',
    },

    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/8.jpg',
      name: 'Lose Control',
      author: 'Hedley',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/9.jpg',
      name: '倩女幽魂',
      author: '张国荣',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/10.jpg',
      name: '北京北京',
      author: '汪峰',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/11.jpg',
      name: '苦笑',
      author: '汪苏泷',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/12.jpg',
      name: '一生所爱',
      author: '卢冠廷 / 莫文蔚',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/13.jpg',
      name: '月半小夜曲',
      author: '李克勤',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/14.jpg',
      name: 'Rolling in the Deep',
      author: 'Adele',
    },
    {
      img: 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/15.jpg',
      name: '海阔天空',
      author: 'Beyond',
    }
  ]

  @Local playIndex: number = -1

  @Builder
  songItem (img: string, name: string, author: string, index:number) {
    ListItem() {
      Row() {
        // 图片
        Stack() {
          Image(img)
            .width(80)
            .margin({
              right: 10
            })
            .border({
              radius: 8
            })
          if (this.playIndex === index) {
            Image($r('app.media.wave'))
              .width(24)
          }
        }
        // 歌曲信息
        Column() {
          Text(name)
            .fontColor(Color.White)
            .fontWeight(700)
            .width('100%')
            .margin({
              bottom: 15
            })
          // VIP及作者
          Row() {
            Text('VIP')
              .border({
                width: 1,
                color: Color.Orange,
                radius: 12
              })
              .padding({
                top: 3,
                bottom: 3,
                left: 5,
                right: 5
              })
              .margin({
                right: 10
              })
              .fontColor(Color.White)
            Text(author)
              .fontColor(Color.Gray)
          }
          .width('100%')
        }
        .layoutWeight(1)
        // 更多
        Image($r("app.media.ic_more"))
          .height(30)
          .fillColor(Color.White)
      }
      .width('100%')
      .height(80)
      .padding({
        left: 10,
        right: 10
      })
      .margin({
        bottom: 10
      })
      //.backgroundColor(Color.Pink)
      .onClick(() => {
        this.playIndex = index
      })
    }
  }

  build() {
    Column() {
      Text('猜你喜欢')
        .fontColor(Color.White)
        .width('100%')
        .margin({
          bottom: 10
        })
      List() {
        ForEach(this.songs, (item: SongItemType, index: number) => {
          this.songItem(item.img, item.name, item.author, index)
        })
      }
      .scrollBar(BarState.Off)
    }
    .width('100%')
    .height('100%')
    .padding({
      left: 10,
      right: 10
    })
    .backgroundColor(Color.Black)
    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
  }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值