【ArkTS】鸿蒙开发 @State状态装饰器修饰变量的用法(实现在用户界面数量的加减)

本文介绍了如何在构建动态界面时使用状态修饰器管理变量,确保UI随变量值的变化实时更新。通过实例展示了如何定义状态变量、显示和修改数值,以及在可编辑标题栏中应用这些概念。
摘要由CSDN通过智能技术生成

 引言:


如果希望构建一个动态的、有交互的界面,就需要引入“状态”的概念。

状态顾名思义,是一个随时可能更新,随时会变化的东西。


此状态指UI的状态,即我们开发的用户界面的状态,普通let引出的普通变量,其值发生改变时,无法引起界面的重新渲染,界面状态不发生变化。

状态修饰器修饰的变量发生变化会引起UI的渲染刷新

如果不使用状态变量,UI只能在初始化时渲染,后续将不会再刷新。

举例:用被状态修饰器修饰的变量来表示加减变化

实现步骤

1.用状态修饰器修饰一个变量


num 定义状态变量的位置不能搞错

2.将该变量的值显示到用户界面上


(num初始定义的是number 型 需要转换成字符串型)

Text(this.num.toString())
          .fontSize(30)
          .height(50)
          .padding({left: 10, right: 10})
          .border({width: {top: 5, bottom: 5}, color: '#999'})

3.通过onClick点击事件改变num的值

通过其自身变量值改变UI重新渲染的特性使其时刻在界面上保持最新值

 //减号
        Text('-')
          .width(50)
          .height(50)
          .border({width: 5, color: '#999', radius: {topLeft: 3, bottomLeft:3}})
          .textAlign(TextAlign.Center)
          .fontSize(30)
          .onClick(()=>{
            if(this.num<1){}
            else {
              this.num -= 1
            }
          })

        //加号
        Text('+')
          .width(50)
          .height(50)
          .border({width: 5, color: '#999', radius: {topRight: 3, bottomRight: 3}})
          .textAlign(TextAlign.Center)
          .fontSize(30)
          .onClick(()=>{
            this.num+=1
          })

完整代码

import { EditableTitleBar } from '@ohos.arkui.advanced.EditableTitleBar'
Text(this.num.toString())
  .fontSize(30)
  .height(50)
  .padding({left: 10, right: 10})
  .border({width: {top: 5, bottom: 5}, color: '#999'})
@Entry
@Component
struct Index {
@State num:number=1


  build() {
    Column() {
      Row() {
        //减号
        Text('-')
          .width(50)
          .height(50)
          .border({width: 5, color: '#999', radius: {topLeft: 3, bottomLeft:3}})
          .textAlign(TextAlign.Center)
          .fontSize(30)
          .onClick(()=>{
            if(this.num<1){}
            else {
              this.num -= 1
            }
          })
        //数量
        Text(this.num.toString())
          .fontSize(30)
          .height(50)
          .padding({left: 10, right: 10})
          .border({width: {top: 5, bottom: 5}, color: '#999'})
        //加号
        Text('+')
          .width(50)
          .height(50)
          .border({width: 5, color: '#999', radius: {topRight: 3, bottomRight: 3}})
          .textAlign(TextAlign.Center)
          .fontSize(30)
          .onClick(()=>{
            this.num+=1
          })

      }
      //.translate({x:85})
      Image($r('app.media.bsheep'))
        .width(300)
        .offset({ y: 30 })
    }
    .width('100%')
    .height('100%')

    .padding(20)
  }

}

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值