harmony OS NEXT-Progress组件概述
2025-03-22 15:42:28
137次阅读
0个评论
鸿蒙Harmony-Progress组件概述
1.1Progress组件概述
- 作用:显示操作或任务的进度,支持线性,环形,刻度等多种样式
- 适用场景:文件上传/下载、任务完成度、系统状态反馈等
2.1基础属性(参考官方文档)
[https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-progress-V5]:
2.1.1 参数详解
参数列表
参数名 | 类型 | 必填 | 默认值 | 说明 | 卡片能力 |
---|---|---|---|---|---|
value | number |
是 | 0 |
当前进度值。若设置 <0 则自动置为 0 ;若 >total 则置为 total 。 |
支持(API 9+) |
total | number |
否 | 100 |
总进度长度。若设置 ≤0 则自动置为 100 。 |
支持(API 9+) |
type8+ | ProgressType |
否 | ProgressType.Linear |
进度条类型:Linear (线性)/Ring (环形)/Eclipse (圆形)/Scale (刻度) |
支持(API 9+) |
style (deprecated) | ProgressStyle |
否 | ProgressStyle.Linear |
进度条样式(已废弃,建议使用 type 替代) |
支持(API 9+,但不再推荐使用) |
2.1.2关键说明
value
-
动态绑定:可通过变量动态更新进度值。
-
范围控制:自动限制在
[0, total]
区间内。 -
示例:
@State progressValue: number = 30; Progress({ value: this.progressValue })
3.1使用示例
下面我通过组件封装的案例给大家演示
- 封装loading组件
@Component
struct Loading {
@State present: number = 10
private timeId: number | null = null
aboutToAppear(): void {
//清除已有的定时器,防止重复调用
if (this.timeId) {
clearInterval(this.timeId)
}
//创建新的定时器
this.timeId = setInterval(() => {
if (this.present >= 100) {
this.present = 0
} else {
this.present++
}
}, 30)
}
aboutToDisappear(): void {
if (this.timeId) {
clearTimeout(this.timeId)
this.timeId = null
}
}
build() {
Column() {
Progress({
value: this.present,
type: ProgressType.Ring,
total: 100
})
}
}
}
export default Loading
- 展示页面
import Loading from './Loading';
@Entry
@Component
struct Test3 {
@State
isShow: boolean = true
//存储定时器Id 用于清除定时器
private timerId: number | null = null
aboutToAppear(): void { //生命周期钩子,当组件即将显示时触发
this.timerId = setTimeout(() => { //设置一个一次性定时器 只会执行一次
this.isShow = false //3s将isShow标志设置为false
}, 3 * 1000) //延迟时间:3s (3000ms)
}
//避免组件在3s内被销毁,造成内存泄漏或报错 (比如用户快速切换页面)
//修复:在组建销毁时清除定时器 添加生命周期销毁钩子
aboutToDisappear(): void {
if (this.timerId) {
clearTimeout(this.timerId) //清除定时器
this.timerId = null
}
}
build() {
Column() {
if (this.isShow) {
Loading()
}
}.width('100%')
.height('100%')
}
}
- 效果展示
00
- 0回答
- 1粉丝
- 0关注
相关话题
- harmony OS NEXT-Refresh 容器组件
- harmony OS NEXT-基本组件结构
- harmony OS NEXT-常用组件及其导航
- harmony OS NEXT-ArkTS组件结构和状态管理
- harmony OS NEXT-Image组件如何引用网络图片
- harmony OS NEXT-启动页开发
- harmony OS NEXT-Navagation基本用法
- Harmony OS Next应用开发之HTTP请求
- harmony OS NEXT-ArkTs面向对象编程
- harmony OS NEXT–状态管理器–@State详解
- harmony OS NEXT-应用状态-AppStorage详细介绍
- harmony OS NEXT-评论功能小demo
- harmony OS NEXT-音频录制与播放模块
- Harmony OS Next应用“丁斗口算”开发记录(1)
- Harmony OS Next应用“丁斗口算”开发记录(2)