OpenHarmony进度条显示最新状态
一、核心思路
二、标准用法代码示例 ArkTS
@Entry
@Component
struct Demo {
@State progressValue: number = 0 // 0~100
@State isIndeterminate: boolean = true // true=不确定进度,false=确定进度
@State statusText: string = '准备中...'
build() {
Column({ space: 12 }) {
// 1) 不确定进度(转圈)
if (this.isIndeterminate) {
LoadingProgress()
.width(48)
.height(48)
.color('#1890FF')
Text('加载中...')
.fontSize(14)
.fontColor('#666')
}
// 2) 确定进度(线性)
if (!this.isIndeterminate) {
Progress({ value: this.progressValue, total: 100 })
.width('100%')
.height(8)
.color('#1890FF')
.backgroundColor('#E0E0E0')
.style({ strokeWidth: 8 })
Text(`${this.progressValue}% - ${this.statusText}`)
.fontSize(14)
.fontColor('#333')
}
Button(this.isIndeterminate ? '开始' : '重置')
.onClick(() => this.startOrReset())
}
.padding(16)
.width('100%')
}
private startOrReset() {
if (this.isIndeterminate) {
// 模拟进入确定进度
this.isIndeterminate = false
this.progressValue = 0
this.statusText = '下载中...'
this.tick()
} else {
// 重置为不确定进度
this.isIndeterminate = true
this.progressValue = 0
this.statusText = '准备中...'
}
}
private tick() {
if (this.progressValue >= 100) {
this.statusText = '完成'
return
}
setTimeout(() => {
// 模拟进度增长(实际替换为下载/任务回调)
this.progressValue = Math.min(this.progressValue + 10, 100)
this.statusText = `下载中 ${this.progressValue}%`
this.tick()
}, 500)
}
}
要点:用**@State驱动UI刷新;不确定进度用LoadingProgress**,确定进度用Progress并设置value/total;通过setTimeout/异步回调持续更新即可实时反映最新状态。
三、常见场景与组件选择
四、进阶与排错
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。