鸿蒙Counter计数器组件的用法(附带实例)
HarmonyOS NEXT 的容器组件是界面设计中用于组织和布局子组件的基础元素。这些组件能够容纳其他组件,并按照特定的排列方式展示它们。
Counter 是声明式开发范式目前可供选择的容器组件之一,Counter 是计数器组件,提供相应的增加或者减少计数的操作。
Counter 组件的示例代码如下:

图 1 Counter 组件的效果
Counter 是声明式开发范式目前可供选择的容器组件之一,Counter 是计数器组件,提供相应的增加或者减少计数的操作。
Counter 组件的示例代码如下:
Row() {
Text('增加按钮禁用').fontSize(20).fontWeight(800).width('100%').height(30)
}
Row() {
Counter() {
Text(this.upValue.toString())
}.margin(100)
.enableInc(!(this.upValue>10)) // 设置增加按钮禁用或使能。默认值为true
.onInc(() => { // 监听数值增加事件
this.upValue++
})
.onDec(() => { // 监听数值减少事件
this.upValue--
})
}
Row() {
Text('减少按钮禁用').fontSize(20).fontWeight(800).width('100%').height(30)
}
Row() {
Counter() {
Text(this.downValue.toString())
}.margin(100)
.enableDec(!(this.downValue<=0)) // 设置增加按钮禁用或使能。默认值为true
.onInc(() => {
this.downValue++
})
.onDec(() => {
this.downValue--
})
}
Counter 的效果如下图所示:
图 1 Counter 组件的效果
ICP备案:
公安联网备案: