请参考以下示例:
[@CustomDialog](/user/CustomDialog)
struct CustomDialogExample {
controller?: CustomDialogController
[@State](/user/State) myTimerModifier: MyTextTimerModifier = new MyTextTimerModifier()
textTimerController: TextTimerController = new TextTimerController()
count:number = 1000*60
[@State](/user/State) format: string = 'ss'
[@State](/user/State) utc: number = 0
[@State](/user/State) elapsedTime: number = 0
cancel: () => void = () => {
}
confirm: () => void = () => {
}
build() {
Column() {
if ((Math.max(this.count / 1000 - this.elapsedTime))>0){
TextTimer({isCountDown: true, count: this.count, controller: this.textTimerController})
.contentModifier(this.myTimerModifier)
.format(this.format)
.onAppear(()=>{
this.textTimerController.start()
})
.onTimer((utc: number, elapsedTime: number) => {
this.elapsedTime = elapsedTime
console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)
})
}else {
Button('重新发送')
.onClick(()=>{
this.elapsedTime = 0
this.textTimerController.start()
})
}
}
}
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct CustomDialogUser {
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample({
cancel: ()=> { this.onCancel() },
confirm: ()=> { this.onAccept() }
}),
cancel: this.existApp,
autoCancel: true,
onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss()
}
if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
dismissDialogAction.dismiss()
}
},
alignment: DialogAlignment.Center,
offset: { dx: 0, dy: -20 },
customStyle: false,
cornerRadius: 20,
width: 300,
height: 200,
borderWidth: 1,
borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
backgroundColor: Color.White,
shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
})
// 在自定义组件即将析构销毁时将dialogController置空
aboutToDisappear() {
this.dialogController = null // 将dialogController置空
}
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
existApp() {
console.info('Click the callback in the blank area')
}
build() {
Column() {
Button('click me')
.onClick(() => {
if (this.dialogController != null) {
this.dialogController.open()
}
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
class MyTextTimerModifier implements ContentModifier<TextTimerConfiguration> {
constructor() {
}
applyContent() : WrappedBuilder<[TextTimerConfiguration]>
{
return wrapBuilder(buildTextTimer)
}
}
[@Builder](/user/Builder) function buildTextTimer(config: TextTimerConfiguration) {
Column() {
Text((Math.max(config.count / 1000 - config.elapsedTime )+'')).fontSize(22).fontColor(Color.Red)
.width(100).height(40).borderRadius(10).borderColor(Color.Brown).backgroundColor(Color.Gray)
.textAlign(TextAlign.Center)
}
}