可以这样修改string资源:{"name": "str\_retry","value": "请在%d秒后重试"},this.countdown这个应该是getStringSync这个方法的参数而不是$r()的参数,您位置放错了;另外每次数值变化都要重新赋值@Entry @Component struct Index { // 倒计时 @State countdown: number = 60; // 属性字符串 mutableStyledString: MutableStyledString = new MutableStyledString(getContext().resourceManager.getStringSync($r('app.string.str_retry'),this.countdown)); controller: TextController = new TextController(); async onPageShow() { this.controller.setStyledString(this.mutableStyledString) } // 开始倒计时 aboutToAppear() { setInterval(() => { this.countdown--; this.mutableStyledString = new MutableStyledString(getContext().resourceManager.getStringSync($r('app.string.str_retry'),this.countdown)); this.controller.setStyledString(this.mutableStyledString) }, 1000) } build() { Column() { Text(undefined, { controller: this.controller }).key('mutableStyledString') // 始终显示“请在%d秒后重试” // Text($r('app.string.str_retry', this.countdown)) // 显示“请在60秒后重试”且正常倒数 } } }
可以这样修改string资源:{"name": "str\_retry","value": "请在%d秒后重试"},this.countdown这个应该是getStringSync这个方法的参数而不是$r()的参数,您位置放错了;另外每次数值变化都要重新赋值