1、概 述
在我们项目开发过程中,时常会遇到时间选择器的场景(如下图),ArkUI为我们提供了一套时间选择对话框,本文针对时间选择对话框的使用做简单介绍。

2、接口说明
我们可根据24小时的时间区间,创建时间滑动选择器弹窗,将时间信息清晰地展示在弹出的窗口上。
时间滑动选择器弹窗通过UIContext中的showTimePickerDialog接口实现,showTimePickerDialog接口定义如下:
showTimePickerDialog(options: TimePickerDialogOptions): void
其中TimePickerDialogOptions是入参选项,可以支持的属性罗列如下:
| 名称 | 类型 | 说明 |
| selected | Date | 设置选中项的时间。默认值:当前系统时间从API version 10开始,该参数支持$$双向绑定变量。 |
| format | TimePickerFormat | 指定需要显示的TimePicker的格式。默认值:TimePickerFormat.HOUR_MINUTE |
| useMilitaryTime | boolean | 展示时间是否为24小时制,默认为12小时制。默认值:false |
| disappearTextStyle | PickerTextStyle | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。默认值:{color: '#ff182431',font: {size: '14fp',weight: FontWeight.Regular}} |
| textStyle | PickerTextStyle | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。默认值:{color: '#ff182431',font: {size: '16fp',weight: FontWeight.Regular}} |
| selectedTextStyle | PickerTextStyle | 设置选中项的文本颜色、字号、字体粗细。默认值:{color: '#ff007dff',font: {size: '20vp',weight: FontWeight.Medium}} |
| acceptButtonStyle | PickerDialogButtonStyle | 设置确认按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。 |
| cancelButtonStyle | PickerDialogButtonStyle | 设置取消按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。 |
| alignment | DialogAlignment | 弹窗在竖直方向上的对齐方式。默认值:DialogAlignment.Default |
| offset | Offset | 弹窗相对alignment所在位置的偏移量。默认值:{ dx: 0 , dy: 0 } |
| maskRect | Rectangle | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。默认值:{ x: 0, y: 0, width: '100%', height: '100%' } |
| onAccept | (value: TimePickerResult) => void | 点击弹窗中的“确定”按钮时触发该回调。 |
| onCancel | () => void | 点击弹窗中的“取消”按钮时触发该回调。 |
| onChange | (value: TimePickerResult) => void | 滑动弹窗中的选择器使当前选中时间改变时触发该回调。 |
| backgroundColor | ResourceColor | 弹窗背板颜色。默认值:Color.Transparent |
| backgroundBlurStyle | BlurStyle | 弹窗背板模糊材质。默认值:BlurStyle.COMPONENT_ULTRA_THICK |
| onDidAppear | () => void | 弹窗弹出时的事件回调。 |
| onDidDisappear | () => void | 弹窗消失时的事件回调。 |
| onWillAppear | () => void | 弹窗显示动效前的事件回调。 |
| onWillDisappear | () => void | 弹窗退出动效前的事件回调。 |
| shadow | ShadowOptions | ShadowStyle | 设置弹窗背板的阴影。当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM |
| dateTimeOptions | DateTimeOptions | 设置时分是否显示前置0,目前只支持设置hour和minute参数。默认值:hour: 24小时制默认为"2-digit",即有前置0;12小时制默认为"numeric",即没有前置0。minute: 默认为"2-digit",即有前置0。 |
3、案 例
👉🏻 案例1:通过自定义配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现自定义文本以及按钮样式,效果如下:

代码如下(30 ~ 51行):
const acceptButtonStyle: PickerDialogButtonStyle = {fontColor: '#2787d9',fontSize: '16fp',backgroundColor: '#f7f7f7',borderRadius: 10}const selectedTextStyle: PickerTextStyle = { color: '#004aaf', font: { size: '18fp', weight: FontWeight.Regular } };const cancelButtonStyle: PickerDialogButtonStyle = {fontColor: Color.Red,fontSize: '16fp',backgroundColor: '#f7f7f7',borderRadius: 10}const textStyle: PickerTextStyle = { color: '#2787d9', font: { size: '14fp', weight: FontWeight.Normal } }class SelectTime{selectTime: Date = new Date('2020-12-25T08:30:00')hours(h:number,m:number){this.selectTime.setHours(h,m)}}@Entry@Componentstruct TimePickerDialogExample {@State selectTime: Date = new Date('2023-12-25T08:30:00');build() {Column() {Button('显示时间选择器对话框').margin(30).onClick(() => {this.getUIContext().showTimePickerDialog({selected: this.selectTime,textStyle,selectedTextStyle,acceptButtonStyle,cancelButtonStyle,onAccept: (value: TimePickerResult) => {// 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间let time = new SelectTime();if(value.hour&&value.minute){time.hours(value.hour, value.minute)}console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))},onCancel: () => {console.info("TimePickerDialog:onCancel()")},onChange: (value: TimePickerResult) => {console.info("TimePickerDialog:onChange()" + JSON.stringify(value))}})})}.width('100%').margin({ top: 5 })}}
👉🏻 案例 2:24小时制显示(没有上午/下午的切换),显示效果如下:

代码如下:
@Entry@Componentstruct TimePickerDialogExample {@State selectTime: Date = new Date('2023-12-25T08:30:00');build() {Column() {Button("时间弹窗 24小时制").margin(20).onClick(() => {TimePickerDialog.show({selected: this.selectTime,useMilitaryTime: true,onAccept: (value: TimePickerResult) => {if (value.hour != undefined && value.minute != undefined) {this.selectTime.setHours(value.hour, value.minute)console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))}},onCancel: () => {console.info("TimePickerDialog:onCancel()")},onChange: (value: TimePickerResult) => {console.info("TimePickerDialog:onChange()" + JSON.stringify(value))},onDidAppear: () => {console.info("TimePickerDialog:onDidAppear()")},onDidDisappear: () => {console.info("TimePickerDialog:onDidDisappear()")},onWillAppear: () => {console.info("TimePickerDialog:onWillAppear()")},onWillDisappear: () => {console.info("TimePickerDialog:onWillDisappear()")}})})}.width('100%').margin({ top: 5 })}}
