无法在卡片正常使用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
相关问题
地图组件无法正常展示
503浏览 • 1回复 待解决
无法正常使用开发工具Pycharm
30761浏览 • 47回复 已解决
HarmonyOS AKI绑定失败,无法正常使用
374浏览 • 1回复 待解决
HarmonyOS TextTimer 计时器使用问题
91浏览 • 1回复 待解决
HarmonyOS 地图无法正常加载
39浏览 • 1回复 待解决
HarmonyOS WebView无法正常渲染
77浏览 • 1回复 待解决
分析埋点无法正常上报
1860浏览 • 1回复 待解决