【练习】使用DevEco Studio编写点赞案例

效果展示

效果说明

如果当前处于未点赞状态,那么点击将点赞数+1,并且颜色变为粉色;如果当前已经处于点赞状态,那么点击点赞按钮,点赞数-1,并且颜色恢复为最初的颜色。

知识点

点击交互事件onClick

http://t.csdnimg.cn/KkxaHicon-default.png?t=N7T8http://t.csdnimg.cn/KkxaH

状态变量

http://t.csdnimg.cn/KkxaHicon-default.png?t=N7T8http://t.csdnimg.cn/KkxaH

代码展示

@Entry
@Component
struct Index {
  @State num: number = 8888;
  @State color: string = "#777"
  @State isLike: boolean = false

  build() {
    Column() {
      Column() {
        Image($r("app.media.eyes"))
          .width("100%")
          .borderRadius({ topLeft: 15, topRight: 15 })
        Column({ space: 10 }) {
          Text("考眼力又来了你能看到几只鸭子?")
          Row() {
            Image($r("app.media.avatar"))
              .margin({ right: 5 })
              .width(30)
            Text("视野联航眼睛")
              .fontColor("#777")
              .fontSize(14)
              .layoutWeight(1)
            Image($r("app.media.ic_love"))
              .width(20)
              .fillColor(this.color)
              .onClick(() => {
                this.isLike = !this.isLike
                if (this.isLike) {
                  this.num++
                  this.color = "#F57792"
                } else {
                  this.num--
                  this.color = "#777"
                }
                console.log(this.isLike.toString(), this.color)
              })
            Text(this.num.toString())
              .fontColor(this.color)
              .fontSize(14)
          }
          .width("100%")
        }
        .padding(10)
      }
      .borderRadius(15)
      .width(200)
      .backgroundColor("#fff")

    }
    .padding(20)
    .height("100%")
    .width("100%")
    .backgroundColor("#ccc")
  }
}
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值