鸿蒙 ArkTS 实战:洗手液补充记录的状态建模、算法与交互实现
鸿蒙 ArkTS 实战:洗手液补充记录的状态建模、算法与交互实现
前言
洗手液补充记录用于记录当前百分比、补充比例和累计补充次数。
项目通过 ArkTS、ArkUI 和 @State 管理 level、refill、count。
核心是 百分比上下限、补充累计和模拟消耗,默认数据为余量 60%、补充 30%。
页面展示当前余量和累计补充次数。
图 1:DevEco Studio 中的 ArkTS 开发环境。
页面结果应能从当前状态完整复算。
边界条件需要落到真实代码路径。
补充后最高为百分之百。
本文只讨论源码已实现能力。
一、业务对象
1.1 数据结构
洗手液补充记录使用 level、refill、count 支撑业务对象。
补充后最高为百分之百。
1.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
1.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
1.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 业务对象维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
二、状态字段
2.1 数据结构
洗手液补充记录使用 level、refill、count 支撑状态字段。
模拟使用后最低为零。
2.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
2.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
2.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
三、核心规则
3.1 数据结构
洗手液补充记录使用 level、refill、count 支撑核心规则。
count 只在补充时增加。
3.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
3.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
3.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
四、输入控件
4.1 数据结构
洗手液补充记录使用 level、refill、count 支撑输入控件。
补充后最高为百分之百。
4.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
4.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
4.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 输入控件维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
五、事件回调
5.1 数据结构
洗手液补充记录使用 level、refill、count 支撑事件回调。
模拟使用后最低为零。
5.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
5.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
5.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
六、边界保护
6.1 数据结构
洗手液补充记录使用 level、refill、count 支撑边界保护。
count 只在补充时增加。
6.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
6.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
6.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 洗手液补充记录保存真实状态。
- level参与核心逻辑。
- 当前余量和累计补充次数形成反馈。
- 百分比上下限、补充累计和模拟消耗保护输出。
七、状态反馈
7.1 数据结构
洗手液补充记录使用 level、refill、count 支撑状态反馈。
补充后最高为百分之百。
7.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
7.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
7.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
八、默认运行
8.1 数据结构
洗手液补充记录使用 level、refill、count 支撑默认运行。
模拟使用后最低为零。
8.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
8.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
8.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 默认运行维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
九、连续迁移
9.1 数据结构
洗手液补充记录使用 level、refill、count 支撑连续迁移。
count 只在补充时增加。
9.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
9.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
9.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
十、数组或数值更新
10.1 数据结构
洗手液补充记录使用 level、refill、count 支撑数组或数值更新。
补充后最高为百分之百。
10.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
10.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
10.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
十一、声明式刷新
11.1 数据结构
洗手液补充记录使用 level、refill、count 支撑声明式刷新。
模拟使用后最低为零。
11.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
11.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
11.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 洗手液补充记录保存真实状态。
- level参与核心逻辑。
- 当前余量和累计补充次数形成反馈。
- 百分比上下限、补充累计和模拟消耗保护输出。
十二、布局层级
12.1 数据结构
洗手液补充记录使用 level、refill、count 支撑布局层级。
count 只在补充时增加。
12.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
12.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
12.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 布局层级维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
十三、颜色语义
13.1 数据结构
洗手液补充记录使用 level、refill、count 支撑颜色语义。
补充后最高为百分之百。
13.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
13.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
13.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
十四、异常输入
14.1 数据结构
洗手液补充记录使用 level、refill、count 支撑异常输入。
模拟使用后最低为零。
14.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
14.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
14.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
十五、验证场景
15.1 数据结构
洗手液补充记录使用 level、refill、count 支撑验证场景。
count 只在补充时增加。
15.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
15.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
15.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
十六、性能表现
16.1 数据结构
洗手液补充记录使用 level、refill、count 支撑性能表现。
补充后最高为百分之百。
16.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
16.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
16.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 性能表现维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
十七、维护方式
17.1 数据结构
洗手液补充记录使用 level、refill、count 支撑维护方式。
模拟使用后最低为零。
17.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
17.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
17.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
十八、代码职责
18.1 数据结构
洗手液补充记录使用 level、refill、count 支撑代码职责。
count 只在补充时增加。
18.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
18.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
18.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
- 洗手液补充记录保存真实状态。
- level参与核心逻辑。
- 当前余量和累计补充次数形成反馈。
- 百分比上下限、补充累计和模拟消耗保护输出。
十九、运行结论
19.1 数据结构
洗手液补充记录使用 level、refill、count 支撑运行结论。
补充后最高为百分之百。
19.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
19.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
19.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
二十、项目启示
20.1 数据结构
洗手液补充记录使用 level、refill、count 支撑项目启示。
模拟使用后最低为零。
20.2 业务规则
补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%
level 及相关表达式承担核心行为。
20.3 交互刷新
控件回调更新状态并显示当前余量和累计补充次数。
ArkUI 根据依赖关系刷新。
20.4 边界结论
百分比上下限、补充累计和模拟消耗
结果因此保持稳定。
| 项目启示维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
二十一、真实源码分段
21.1 源码片段 1
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.2 源码片段 2
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.3 源码片段 3
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.4 源码片段 4
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.5 源码片段 5
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.6 源码片段 6
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.7 源码片段 7
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
21.8 源码片段 8
以下代码来自当前项目。
@Entry @Component struct Index{@State level:number=60;@State refill:number=30;@State count:number=0;build(){Column(){Text('洗手液补充记录').fontSize(27).fontWeight(FontWeight.Bold).width('100%');Text('记录当前余量和每次补充量').fontColor('#667085').width('100%').margin({top:6,bottom:22});Text(this.level+'%').fontSize(52).fontWeight(FontWeight.Bold).fontColor(this.level<=20?'#B42318':'#175CD3');Slider({value:this.level,min:0,max:100,step:5}).onChange((v:number)=>this.level=Math.round(v)).width('100%');Text('本次补充:'+this.refill+'%').width('100%').margin({top:20});Slider({value:this.refill,min:5,max:100,step:5}).onChange((v:number)=>this.refill=Math.round(v)).width('100%');Button('登记补充').width('100%').height(48).margin({top:22}).onClick(()=>{this.level=Math.min(100,this.level+this.refill);this.count++});Button('模拟使用 10%').width('100%').margin({top:10}).backgroundColor('#EEF2F6').fontColor('#344054').onClick(()=>this.level=Math.max(0,this.level-10));Text('累计补充 '+this.count+' 次').fontColor('#667085').margin({top:14})}.width('100%').height('100%').padding(22).backgroundColor('#FFFFFF')}}
该片段参与记录当前百分比、补充比例和累计补充次数。
二十二、运行案例
22.1 默认数据
使用余量 60%、补充 30%。
观察 level、refill、count 与结果。
确认当前余量和累计补充次数可复算。
22.2 最小边界
百分比上下限、补充累计和模拟消耗
观察 level、refill、count 与结果。
确认当前余量和累计补充次数可复算。
22.3 临界输入
围绕百分比上下限、补充累计和模拟消耗验证阈值。
观察 level、refill、count 与结果。
确认当前余量和累计补充次数可复算。
22.4 连续操作
连续执行 level。
观察 level、refill、count 与结果。
确认当前余量和累计补充次数可复算。
| 验证维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | level、refill、count | 保存数据 |
| 规则 | 补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10% | 生成反馈 |
| 边界 | 百分比上下限、补充累计和模拟消耗 | 保护结果 |
| 结果 | 当前余量和累计补充次数 | 呈现结论 |
二十三、技术取舍
23.1 本地状态
洗手液补充记录围绕百分比上下限、补充累计和模拟消耗选择本地状态。
当前职责集中且可追踪。
23.2 具名函数
洗手液补充记录围绕百分比上下限、补充累计和模拟消耗选择具名函数。
当前职责集中且可追踪。
23.3 声明式组件
洗手液补充记录围绕百分比上下限、补充累计和模拟消耗选择声明式组件。
当前职责集中且可追踪。
23.4 清晰边界
洗手液补充记录围绕百分比上下限、补充累计和模拟消耗选择清晰边界。
当前职责集中且可追踪。
二十四、总结
洗手液补充记录使用鸿蒙 ArkTS 完成了“记录当前百分比、补充比例和累计补充次数”闭环。
核心规则是:补充用 Math.min 把 level 限制为 100,模拟使用用 Math.max 扣减 10%。
状态字段为 level、refill、count。
状态建模、业务函数 与 声明式界面 共同保证结果稳定。
相关资源:
更多推荐



所有评论(0)