1.Dialog 弹框的整理
import { TipsDialog, SelectDialog, ConfirmDialog, AlertDialog, LoadingDialog, CustomContentDialog } from '@kit.ArkUI'
export class DialogUtils {
static showAlertDialog(title: string, message: string, cancelText: string, sureText: string, sureClicked: () => void) :CustomDialogController{
let dialogControllerConfirm: CustomDialogController = new CustomDialogController({
builder: AlertDialog({
primaryTitle: title,
content: message,
primaryButton: {
value: cancelText,
action: () => {
},
},
secondaryButton: {
value: sureText,
role: ButtonRole.ERROR,
action: () => {
sureClicked()
}
},
}),
})
return dialogControllerConfirm
}
static showControllerCheckBox(title: string, message: string, isChecked : boolean, arrowClicked: (isChecked:boolean) => void) :CustomDialogController{
let dialogControllerCheckBox: CustomDialogController = new CustomDialogController({
builder: ConfirmDialog({
title: title,
content: message,
// 勾选框选中状态
isChecked: isChecked,
// 勾选框说明文本
checkTips: '禁止后不再提示',
primaryButton: {
value: '禁止',
action: () => {
dialogControllerCheckBox.close()
},
},
secondaryButton: {
value: '允许',
action: () => {
isChecked = false
arrowClicked(isChecked)
dialogControllerCheckBox.close()
}
},
onCheckedChange: () => {
},
}),
autoCancel: true,
alignment: DialogAlignment.Bottom
})
return dialogControllerCheckBox
}
static showControllerWait(message: string) :CustomDialogController{
let dialogControllerProgress: CustomDialogController = new CustomDialogController({
builder: LoadingDialog({
content: message,
themeColorMode: ThemeColorMode.LIGHT // 默认浅色主题
}),
})
return dialogControllerProgress
}
static showCalendarPicker(date: Date, okClick: (newDate:string) => void) {
CalendarPickerDialog.show({
selected: date,
onAccept: (value) => {
console.info("calendar onAccept:" + JSON.stringify(value))
okClick(JSON.stringify(value))
},
onCancel: () => {
},
onChange: (value) => {
console.info("calendar onChange:" + JSON.stringify(value))
},
})
}
static showDatePickerDialog(curDate: Date,startDate: Date,endDate: Date, okClick: (newDate:Date) => void) {
DatePickerDialog.show({ // 建议使用 this.getUIContext().showDatePickerDialog()接口
start: startDate,
end: endDate,
selected: curDate,
showTime:false,// 是否展示时间项,true表示显示时间,false表示不显示
lunar:false,//是否是农历
useMilitaryTime:true,//展示时间是否为24小时制
disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}},
textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}},
selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}},
onDateAccept: (value: Date) => {
okClick(value)
},
onCancel: () => {
},
onDateChange: (value: Date) => {
console.info("DatePickerDialog:onDateChange()" + value.toString())
},
})
}
static showTipsDialog(topRes:ResourceStr,content: string, sureClick: () => void) :CustomDialogController{
let dialogControllerImage: CustomDialogController = new CustomDialogController({
builder: TipsDialog({
imageRes: topRes,
content: content,
primaryButton: {
value: '取消',
action: () => {
console.info('Callback when the first button is clicked')
},
},
secondaryButton: {
value: '确认',
role: ButtonRole.ERROR,
action: () => {
sureClick()
}
}
}),
})
return dialogControllerImage
}
}
2.倒计时工具列
export class CountdownUtil {
private static instance: CountdownUtil | undefined = undefined;
private intervalId: number | undefined = undefined;
private constructor() {}
public static getInstance(): CountdownUtil {
if (!CountdownUtil.instance) {
CountdownUtil.instance = new CountdownUtil();
}
return CountdownUtil.instance;
}
public startCountdown(duration: number, onTick: (remaining: number) => void, onComplete: () => void): void {
if (this.intervalId !== undefined) {
clearInterval(this.intervalId);
}
let remainingTime = duration;
this.intervalId = setInterval(() => {
remainingTime--;
onTick(remainingTime);
if (remainingTime <= 0) {
clearInterval(this.intervalId);
this.intervalId = undefined;
onComplete();
}
}, 1000);
}
public stopCountdown(): void {
if (this.intervalId !== undefined) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
}
}