HarmonyOS Next 纯血星河版【五】:事件交互 - 点击事件 & State状态管理 & 一元运算符、比较、逻辑运算符、以及优先级 & 美团外卖购物车案例

本文详细介绍了HarmonyOSNext中事件处理(onClick)的代码示例,以及状态管理的概念、普通变量与装填变量的差异,通过计数器和模拟抖音点赞案例展示状态绑定。还涉及基本的算术运算符、一元运算符、比较运算符和逻辑运算符的使用,以及一个外卖购物车的综合案例。
摘要由CSDN通过智能技术生成

HarmonyOS Next 纯血星河版【五】

事件 - onClick

一、事件交互 - 点击事件

在这里插入图片描述

代码示例:
@Entry
@Component
struct  OnClick {
  build () {
    Column() {
      Button('点击弹框')
        // 绑定点击事件
        .onClick(() => {
          AlertDialog.show({
            message: '弹框提示'
          })
        })
    }
    .width('100%')
  }
}

在这里插入图片描述

二、总结

在这里插入图片描述

状态管理 - State

一、 什么是 状态管理

在这里插入图片描述

二、 普通变量 和 装填变量的区别

在这里插入图片描述

三、计数器案例

在这里插入图片描述

@Entry
@Component
struct StateTest  {
  @State count: number = 1
  build () {
    Column() {
      Row({ space: 20 }) {
        Button('-')
          .width(80)
          .height(80)
          .borderRadius(40)
          .fontSize(35)
          .onClick(() => {
            if ( this.count > 1) {
              this.count--
            } else {
              AlertDialog.show({
                message: '操作数已达到限制!'
              })
            }
          })
          Text(this.count.toString())
            .fontSize(40)
            .margin({left: 10, right: 10})
        Button('+')
          .width(80)
          .height(80)
          .borderRadius(40)
          .fontSize(35)
          .onClick(() => {
            if ( this.count < 10 ) {
              this.count++
            } else {
              AlertDialog.show({
                message: '操作数已达到限制!'
              })
            }
          })
      }
    }
    .width('100%')
    .height('100%')
    .padding(10)
    .justifyContent(FlexAlign.Center)
  }
}

在这里插入图片描述

四、模拟抖音点赞案例

  • 需求:用户点击爱心,则将触发效果。将数据和视图进行双向绑定。
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码示例:
@Entry
@Component
struct StateTest2 {
  // 创建两个 状态变量 分别是  点在爱心颜色 和 点赞数
  @State number: number = 9999
  @State colorLove: String = "#000003"
  @State colorNumber: String = "#000003"
  build() {
    Column() {
      Column() {
        Image($r('app.media.state_huawei_test'))
          .width('100%')
          .height(200)
          .borderRadius({ topLeft: 10, topRight: 10, bottomLeft: 5, bottomRight: 5})
        Text('HUAWEI:没有退路就是胜利之路!')
          .fontWeight(700)
          .margin({ top: 10, bottom: 10})
        Row() {
          Image($r('app.media.huawei'))
            .width(40).height(40)
            .borderRadius(20)
          Text('HUAWEI')
            .fontSize(18)
            .layoutWeight(1)

          Row({ space: 10}){
            Image($r('app.media.state_love'))
              .width(20)
              .height(20)
              .fillColor(this.colorLove.toString())
              // 绑定点击事件
              .onClick(() => {
                if (this.number == 9999) {
                  // 修改颜色 和 数量
                  this.colorLove = '#FF0000'
                  this.colorNumber = '#FF0000'
                  this.number++
                } else {
                  // 将颜色 和 数值 进行还原
                  this.colorLove = '#000003'
                  this.colorNumber = '#000003'
                  this.number--
                }

              })
            Text(this.number.toString())
              .fontColor(this.colorNumber.toString())
          }
          .width(100)
          .height(50)
          .justifyContent(FlexAlign.End)
        }
        .width('100%')
        .height(50)
      }
      .width(300)
      .padding(10)
      .backgroundColor(Color.White)
      .borderRadius(10)
    }
    .width('100%')
    .height('100%')
    .backgroundColor("#F6F5F2")
  }
}

运算符

基本的算数运算符

在这里插入图片描述

一、赋值运算符

在这里插入图片描述

二、总结

  • 简单的算数运算符如下所示:
    在这里插入图片描述

一元运算符

一、基本使用

在这里插入图片描述

代码示例:
  • 后置
    在这里插入图片描述
  • 前置
    在这里插入图片描述

二、总结

在这里插入图片描述

比较运算符

一、基本的比较符号

在这里插入图片描述

代码示例:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、总结

在这里插入图片描述

逻辑运算符

一、逻辑运算符基本使用

在这里插入图片描述

代码示例:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、总结

在这里插入图片描述

运算优先级

在这里插入图片描述

综合案例 - 外卖购物车

在这里插入图片描述
在这里插入图片描述

代码示例:

@Entry
@Component
struct StateTest3 {
  // 添加状态管理变量
  @State totalPrice: number = 20.2  // 总金额
  @State discountPrice: number = 20.2  // 折扣价
  @State originalPrice: number = 40.4  // 原价
  @State reducedPrice: number = 20.2  // 共减免
  @State countImte : number = 1  // 每个商品选中的个数
  build() {
    Column() {
      // 主体:购物车商品详情列表
      Column() {
        // 商品规格详情列表(List容器,超出范围自动滚动效果)
        List() {
          ListItem() {
            Row() {
              Image($r('app.media.waimai_test_image'))
                .width(110)
                .height(90)
                .borderRadius(15)
              // 商品文字信息区域
              Column() {
                Column() {
                  Text('冲销量 1000 缤纷八果水果捞')
                    .fontColor(Color.Black)
                    .fontSize(14)
                  Text('含 1 份折扣商品')
                    .fontColor('#c3c3c3')
                    .fontSize(12)
                    .margin({ top: 10 })
                }
                .width('100%')
                .alignItems(HorizontalAlign.Start)
                .margin({left: 10, top: 5})
                // 数量规格区域
                Row() {
                  Row() {
                    Text(`${this.discountPrice}`)
                      .fontColor(24)
                      .fontColor(Color.Red)
                    Text(`${this.originalPrice}`)
                      .fontColor('#c3c3c3')
                      .fontSize(12)
                      .margin({ left: 5 })
                      .decoration({
                        type: TextDecorationType.LineThrough,
                        color: '#c3c3c3'
                      })
                  }
                  .width(110)

                  // 加减数量
                  Row() {
                    Text('-')
                      .width(20)
                      .height(20)
                      .border({
                        width: 1,
                        color: '#c3c3c3'
                      })
                      .textAlign(TextAlign.Center)
                      .onClick(() => {
                        if (this.countImte > 1) {
                          this.countImte--
                        } else {
                          console.log(' - ', '不能在减了')
                        }
                      })
                    Text(this.countImte.toString())
                      .width(30)
                      .height(20)
                      .border({
                        width: { top: 1, bottom: 1 },
                        color: '#c3c3c3'
                      })
                      .textAlign(TextAlign.Center)
                    Text('+')
                      .width(20)
                      .height(20)
                      .border({
                        width: 1,
                        color: '#c3c3c3'
                      })
                      .textAlign(TextAlign.Center)
                      .onClick(() => {
                        if (this.countImte < 1000) {
                          this.countImte++
                          // 将价格进行更新
                          // 合计
                          this.totalPrice = this.countImte * (this.originalPrice - this.discountPrice)
                          // 优惠
                          this.reducedPrice = this.countImte * (this.originalPrice - this.discountPrice)
                          console.log('总价为:', this.totalPrice.toFixed(2))
                          console.log('优惠:', this.reducedPrice.toFixed(2))
                        } else {
                          console.log(' + ', '暂无库存')
                        }
                      })
                  }
                  .width('100%')
                  .margin({ top: 10, left: 35})
                }
                .layoutWeight(1)
                .padding(10)
              }
              .width('100%')
              .height(90)
              .borderRadius(15)
              .alignItems(HorizontalAlign.Start)
            }
            .margin({ bottom: 10 })
            .backgroundColor('#ffffff')
            .borderRadius(15)
          }
          .width('100%')

        }
        .width('100%')
        .layoutWeight(1)
        .padding(10)

        // 底部结算项
        Row() {
          Column() {
            Text() {
              Span(`已选择 ${this.countImte}`)
                .fontColor('#cacaca')
                .fontSize(14)
              Span(',合计:')
                .fontSize(14)
              Span(`${this.totalPrice.toFixed(2)}`)
                .fontSize(16)
                .fontColor(Color.Red)
            }

            Text(`共减 ¥ ${this.reducedPrice.toFixed(2)}`)
              .fontColor(Color.Red)
              .fontSize(12)
              .margin(5)
          }
          .layoutWeight(1)
          .alignItems(HorizontalAlign.End)
          .padding({ right: 10 })

          Button('结算外卖')
            .fontColor(Color.Black)
            .backgroundColor('#ffd54a')
        }
        .width('100%')
        .height(80)
        .padding(15)
        .backgroundColor('#ffffff')
      }
      .width('100%')
      .height('100%')
      .backgroundColor('#f3f3f3')
    }
  }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脱发使我稳重

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值