1# 时间滑动选择器弹窗 (TimePickerDialog)
2
3以24小时的时间区间创建时间滑动选择器,展示在弹窗上。
4
5>  **说明:**
6>
7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。
10>
11> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTimePickerDialog](../js-apis-arkui-UIContext.md#showtimepickerdialog)来明确UI的执行上下文。
12
13## TimePickerDialog
14
15### show
16
17static show(options?: TimePickerDialogOptions)
18
19定义时间滑动选择器弹窗并弹出。
20
21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
22
23**系统能力:** SystemCapability.ArkUI.ArkUI.Full
24
25**参数:**
26
27| 参数名  | 类型                                                        | 必填 | 说明                       |
28| ------- | ----------------------------------------------------------- | ---- | -------------------------- |
29| options | [TimePickerDialogOptions](#timepickerdialogoptions对象说明) | 否   | 配置时间选择器弹窗的参数。 |
30
31## TimePickerDialogOptions对象说明
32
33继承自[TimePickerOptions](ts-basic-components-timepicker.md#timepickeroptions对象说明)。
34
35**系统能力:** SystemCapability.ArkUI.ArkUI.Full
36
37| 名称 | 类型 | 必填 | 说明 |
38| -------- | -------- | -------- | -------- |
39| useMilitaryTime | boolean | 否 | 展示时间是否为24小时制,默认为12小时制。<br/>默认值:false<br />**说明:** 当展示时间为12小时制时,上下午与小时无联动关系。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
40| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '14fp', <br/>weight: FontWeight.Regular<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
41| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '16fp', <br/>weight: FontWeight.Regular<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
42| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20vp', <br/>weight: FontWeight.Medium<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
43| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置确认按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
44| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置取消按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
45| alignment<sup>10+</sup>  | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否   | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
46| offset<sup>10+</sup>     | [Offset](ts-types.md#offset) | 否     | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{&nbsp;dx:&nbsp;0&nbsp;,&nbsp;dy:&nbsp;0&nbsp;}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
47| maskRect<sup>10+</sup>| [Rectangle](ts-methods-alert-dialog-box.md#rectangle8类型说明) | 否     | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。<br/>默认值:{ x: 0, y: 0, width: '100%', height: '100%' }<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
48| onAccept | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
49| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
50| onChange | (value: [TimePickerResult](ts-basic-components-timepicker.md#TimePickerResult对象说明)) => void | 否 | 滑动弹窗中的选择器使当前选中时间改变时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
51| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
52| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | 否 | 弹窗背板模糊材质。<br/>默认值:BlurStyle.COMPONENT_ULTRA_THICK<br/>**说明:** <br/>设置为BlurStyle.NONE即可关闭背景虚化。当设置了backgroundBlurStyle为非NONE值时,则不要设置backgroundColor,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
53| onDidAppear<sup>12+</sup> | () => void | 否 | 弹窗弹出时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange)>>onWillDisappear>>onDidDisappear。<br />2.在onDidAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br />3.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。<br />4. 当弹窗入场动效未完成时关闭弹窗,该回调不会触发。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
54| onDidDisappear<sup>12+</sup> | () => void | 否 | 弹窗消失时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange)>>onWillDisappear>>onDidDisappear。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
55| onWillAppear<sup>12+</sup> | () => void | 否 | 弹窗显示动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange)>>onWillDisappear>>onDidDisappear。<br />2.在onWillAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
56| onWillDisappear<sup>12+</sup> | () => void | 否 | 弹窗退出动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange)>>onWillDisappear>>onDidDisappear。<br />2.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
57| shadow<sup>12+</sup>              | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明)&nbsp;\|&nbsp;[ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明) | 否   | 设置弹窗背板的阴影。<br />当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
58| dateTimeOptions<sup>12+</sup> | [DateTimeOptions](../../apis-localization-kit/js-apis-intl.md#datetimeoptions) | 否 | 设置时分是否显示前置0,目前只支持设置hour和minute参数。<br/>默认值:<br/>hour: 24小时制默认为"2-digit",即有前置0;12小时制默认为"numeric",即没有前置0。<br/>minute: 默认为"2-digit",即有前置0。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
59| enableHoverMode<sup>14+</sup>     | boolean | 否   | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
60| hoverModeArea<sup>14+</sup>       | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否   | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN <br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
61
62## 示例
63
64>  **说明:**
65>
66> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTimePickerDialog](../js-apis-arkui-UIContext.md#showtimepickerdialog)来明确UI的执行上下文。
67
68### 示例1(弹出时间选择弹窗)
69
70该示例通过点击按钮弹出时间选择弹窗。
71
72```ts
73// xxx.ets
74@Entry
75@Component
76struct TimePickerDialogExample {
77  private selectTime: Date = new Date('2020-12-25T08:30:00')
78
79  build() {
80    Column() {
81      Button("TimePickerDialog 12小时制")
82        .margin(20)
83        .onClick(() => {
84          TimePickerDialog.show({ // 建议使用 this.getUIContext().showTimePickerDialog()接口
85            selected: this.selectTime,
86            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
87            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
88            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
89            onAccept: (value: TimePickerResult) => {
90              // 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间
91              if (value.hour != undefined && value.minute != undefined) {
92                this.selectTime.setHours(value.hour, value.minute)
93                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
94              }
95            },
96            onCancel: () => {
97              console.info("TimePickerDialog:onCancel()")
98            },
99            onChange: (value: TimePickerResult) => {
100              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
101            },
102            onDidAppear: () => {
103              console.info("TimePickerDialog:onDidAppear()")
104            },
105            onDidDisappear: () => {
106              console.info("TimePickerDialog:onDidDisappear()")
107            },
108            onWillAppear: () => {
109              console.info("TimePickerDialog:onWillAppear()")
110            },
111            onWillDisappear: () => {
112              console.info("TimePickerDialog:onWillDisappear()")
113            }
114          })
115        })
116      Button("TimePickerDialog 24小时制")
117        .margin(20)
118        .onClick(() => {
119          TimePickerDialog.show({
120            selected: this.selectTime,
121            useMilitaryTime: true,
122            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
123            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
124            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
125            onAccept: (value: TimePickerResult) => {
126              if (value.hour != undefined && value.minute != undefined) {
127                this.selectTime.setHours(value.hour, value.minute)
128                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
129              }
130            },
131            onCancel: () => {
132              console.info("TimePickerDialog:onCancel()")
133            },
134            onChange: (value: TimePickerResult) => {
135              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
136            },
137            onDidAppear: () => {
138              console.info("TimePickerDialog:onDidAppear()")
139            },
140            onDidDisappear: () => {
141              console.info("TimePickerDialog:onDidDisappear()")
142            },
143            onWillAppear: () => {
144              console.info("TimePickerDialog:onWillAppear()")
145            },
146            onWillDisappear: () => {
147              console.info("TimePickerDialog:onWillDisappear()")
148            }
149          })
150        })
151    }.width('100%')
152  }
153}
154```
155
156![TimetPickerDialog](figures/TimePickerDialog.gif)
157
158
159### 示例2(自定义样式)
160
161该示例通过配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现了自定义文本以及按钮样式。
162
163```ts
164// xxx.ets
165@Entry
166@Component
167struct TimePickerDialogExample {
168  private selectTime: Date = new Date('2020-12-25T08:30:00')
169
170  build() {
171    Column() {
172      Button("TimePickerDialog 24小时制")
173        .margin(20)
174        .onClick(() => {
175          TimePickerDialog.show({
176            selected: this.selectTime,
177            useMilitaryTime: true,
178            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
179            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
180            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
181            acceptButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Red,
182              fontSize: '26fp', fontWeight: FontWeight.Bolder, fontStyle: FontStyle.Normal, fontFamily: 'sans-serif', backgroundColor:'#80834511',
183              borderRadius: 20 },
184            cancelButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Blue,
185              fontSize: '16fp', fontWeight: FontWeight.Normal, fontStyle: FontStyle.Italic, fontFamily: 'sans-serif', backgroundColor:'#50182431',
186              borderRadius: 10 },
187            onAccept: (value: TimePickerResult) => {
188              if (value.hour != undefined && value.minute != undefined) {
189                this.selectTime.setHours(value.hour, value.minute)
190                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
191              }
192            },
193            onCancel: () => {
194              console.info("TimePickerDialog:onCancel()")
195            },
196            onChange: (value: TimePickerResult) => {
197              console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
198            },
199            onDidAppear: () => {
200              console.info("TimePickerDialog:onDidAppear()")
201            },
202            onDidDisappear: () => {
203              console.info("TimePickerDialog:onDidDisappear()")
204            },
205            onWillAppear: () => {
206              console.info("TimePickerDialog:onWillAppear()")
207            },
208            onWillDisappear: () => {
209              console.info("TimePickerDialog:onWillDisappear()")
210            }
211          })
212        })
213    }.width('100%')
214  }
215}
216```
217
218![TimetPickerDialog](figures/TimePickerDialog_CustomButton.png)
219
220### 示例3(悬停态弹窗)
221
222该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
223
224```ts
225@Entry
226@Component
227struct TimePickerDialogExample {
228  private selectTime: Date = new Date('2020-12-25T08:30:00');
229
230  build() {
231    Column() {
232      Button("TimePickerDialog 12小时制")
233        .margin(20)
234        .onClick(() => {
235          TimePickerDialog.show({ // 建议使用 this.getUIContext().showTimePickerDialog()接口
236            selected: this.selectTime,
237            disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } },
238            textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
239            selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
240            onAccept: (value: TimePickerResult) => {
241              // 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间
242              if (value.hour != undefined && value.minute != undefined) {
243                this.selectTime.setHours(value.hour, value.minute);
244                console.info("TimePickerDialog:onAccept()" + JSON.stringify(value));
245              }
246            },
247            onCancel: () => {
248              console.info("TimePickerDialog:onCancel()");
249            },
250            onChange: (value: TimePickerResult) => {
251              console.info("TimePickerDialog:onChange()" + JSON.stringify(value));
252            },
253            onDidAppear: () => {
254              console.info("TimePickerDialog:onDidAppear()");
255            },
256            onDidDisappear: () => {
257              console.info("TimePickerDialog:onDidDisappear()");
258            },
259            onWillAppear: () => {
260              console.info("TimePickerDialog:onWillAppear()");
261            },
262            onWillDisappear: () => {
263              console.info("TimePickerDialog:onWillDisappear()");
264            },
265            enableHoverMode: true,
266            hoverModeArea: HoverModeAreaType.TOP_SCREEN
267          })
268        })
269    }.width('100%')
270  }
271}
272```
273
274![TimetPickerDialog](figures/TimePickerDialog_HoverMode.gif)
275