无法在卡片正常使用TextTimer组件

想在卡片上呈现倒计时效果,在开发中发现使用TextTimer组件,发现TextTimerController.start无效。

@Component  
struct TextTimerExample {  
  textTimerController: TextTimerController = new TextTimerController()  
  @State format: string = 'mm:ss.SS'  
  
  build() {  
    Column() {  
      TextTimer({ isCountDown: true, count: 30000, controller: this.textTimerController.start() })  
        .format(this.format)  
        .fontColor(Color.Black)  
        .fontSize(20)  
        .onTimer((utc: number, elapsedTime: number) => {  
          console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)  
        })  
    }  
  }  
}
HarmonyOS
2024-10-11 10:24:51
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

1、将卡片改为动态卡片,即"isDynamic": true。参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-ui-widget-configuration-V5

2、Button或者onAppear中使用this.textTimerController.start(),参考代码如下:

const storage = new LocalStorage();  
  
// 绑定  
@Entry(storage)  
@Component  
struct WidgetCard {  
  @LocalStorageProp('formId') formId: string = ''  
  @LocalStorageProp('formDimension') formDimension: number = 2  
  @LocalStorageProp('next') next: number = 0  
  textTimerController: TextTimerController = new TextTimerController()  
  @State format: string = 'mm:ss.SS'  
  
  build() {  
    Column() {  
      TextTimer({ isCountDown: true, count: 30000, controller: this.textTimerController })  
        .format(this.format)  
        .fontColor(Color.Black)  
        .fontSize(20)  
        .onTimer((utc: number, elapsedTime: number) => {  
          console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)  
        })  
        .onAppear(() => {  
          try {  
            console.info('wytest: enter onAppear')  
            this.textTimerController.start()  
          } catch (error) {  
            console.error('wytest: onAppear err.')  
          }  
        })  
  
      Row() {  
        Button("start").onClick(() => {  
          this.textTimerController.start()  
        })  
        Button("pause").onClick(() => {  
          this.textTimerController.pause()  
        })  
        Button("reset").onClick(() => {  
          this.textTimerController.reset()  
        })  
      }  
    }  
  }  
}
分享
微博
QQ
微信
回复
2024-10-11 17:28:25
相关问题
地图组件无法正常展示
446浏览 • 1回复 待解决
无法正常使用开发工具Pycharm
30632浏览 • 47回复 已解决
HarmonyOS AKI绑定失败,无法正常使用
338浏览 • 1回复 待解决
分析埋点无法正常上报
1801浏览 • 1回复 待解决
求助一个关于TextTimer的问题 ?
660浏览 • 1回复 待解决
针对Windows system32 无法正常安装
6577浏览 • 1回复 待解决
webview导航无法正常导航,怎么处理?
5974浏览 • 1回复 待解决
应用卡片内部组件使用装饰器吗?
284浏览 • 1回复 待解决
卡片提供方主进程中主动更新卡片
1565浏览 • 1回复 待解决