@CustomDialog
@Component
export default struct datePickerDialog {
selectedDate: Date = new Date()
controller: CustomDialogController
build() {
Column({ space: 10 }) {
DatePicker({
start: new Date('2001-09-08'),
end: new Date('9999-09-08'),
selected: this.selectedDate
})
.onChange((value: DatePickerResult) => {
this.selectedDate.setFullYear(value.year, value.month, value.day)
})
Row({ space: 10 }) {
Button('Cancel')
.onClick(() => {
this.controller.close()
})
.backgroundColor('#ffeaeef1')
.width(100)
.height(30)
Button('Submit')
.width(100)
.height(30)
.onClick(() => {
// this.selectedDate.getTime()存储毫秒值是因为方便后期监控状态变化
// 便于利用prop、link 或者 state
AppStorage.SetOrCreate('selectedDate', this.selectedDate.getTime())
this.controller.close()
})
}
}
.width('100%')
.padding(10)
}
}
/*
在别的Page中利用controller来opendialog
@StorageProp('selectedDate') selectedDate: number = DateUtil.beginTimeOfDay(new Date())
controller: CustomDialogController = new CustomDialogController({
builder: dateDialog({
selectedDate: new Date()
})
})
*/
内置的DatePickerDialog:
自己写的datePickerDialog(好丑):