1# 日期滑动选择器弹窗 (DatePickerDialog) 2 3根据指定的日期范围创建日期滑动选择器,展示在弹窗上。 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)中的[showDatePickerDialog](../js-apis-arkui-UIContext.md#showdatepickerdialog)来明确UI的执行上下文。 12 13## DatePickerDialog 14 15### show 16 17static show(options?: DatePickerDialogOptions) 18 19定义日期滑动选择器弹窗并弹出。 20 21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ------- | ----------------------------------------------------------- | ---- | -------------------------- | 29| options | [DatePickerDialogOptions](#datepickerdialogoptions对象说明) | 否 | 配置日期选择器弹窗的参数。 | 30 31## DatePickerDialogOptions对象说明 32 33继承自[DatePickerOptions](ts-basic-components-datepicker.md#datepickeroptions对象说明)。 34 35**系统能力:** SystemCapability.ArkUI.ArkUI.Full 36 37| 名称 | 类型 | 必填 | 说明 | 38| -------- | -------- | -------- | -------- | 39| lunar | boolean | 否 | 日期是否显示为农历,true表示显示农历,false表示不显示农历。<br/>默认值:false<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 40| showTime<sup>10+</sup> | boolean | 否 | 是否展示时间项,true表示显示时间,false表示不显示时间。<br/>默认值:false<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 41| useMilitaryTime<sup>10+</sup> | boolean | 否 | 展示时间是否为24小时制,true表示显示24小时制,false表示显示12小时制。<br/>默认值:false<br />**说明:** <br/>当展示时间为12小时制时,上下午与小时无联动关系。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 42| lunarSwitch<sup>10+</sup> | boolean | 否 | 是否展示切换农历的开关,true表示展示开关,false表示不展示开关。<br/>默认值:false<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 43| lunarSwitchStyle<sup>14+</sup> | [LunarSwitchStyle](#lunarswitchstyle14类型说明) | 否 | 设置农历开关的颜色样式。<br/>默认值:{ selectedColor: `$r('sys.color.ohos_id_color_text_primary_actived')`, unselectedColor: `$r('sys.color.ohos_id_color_switch_outline_off')`, strokeColor: Color.White }<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 | 44| 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开始,该接口支持在原子化服务中使用。 | 45| 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开始,该接口支持在原子化服务中使用。 | 46| 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开始,该接口支持在原子化服务中使用。| 47| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](#pickerdialogbuttonstyle12类型说明) | 否 | 设置确认按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 48| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](#pickerdialogbuttonstyle12类型说明) | 否 | 设置取消按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 49| alignment<sup>10+</sup> | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 50| offset<sup>10+</sup> | [Offset](ts-types.md#offset) | 否 | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{ dx: 0 , dy: 0 }<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 51| 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开始,该接口支持在原子化服务中使用。 | 52| onAccept<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。<br />**说明:**<br />从API version 8 开始支持,从 API version 10 开始废弃,建议使用onDateAccept。 | 53| onCancel | [VoidCallback](ts-types.md#voidcallback12) | 否 | 点击弹窗中的“取消”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 54| onChange<sup>(deprecated)</sup> | (value: [DatePickerResult](ts-basic-components-datepicker.md#datepickerresult对象说明)) => void | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。<br />**说明:**<br />从API version 8 开始支持,从 API version 10 开始废弃,建议使用onDateChange。 | 55| onDateAccept<sup>10+</sup> | [Callback](ts-types.md#callback12)\<Date> | 否 | 点击弹窗中的“确定”按钮时触发该回调。<br />**说明:**<br />当showTime设置为true时,回调接口返回值value中时和分为选择器选择的时和分。否则,返回值value中时和分为系统时间的时和分。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 56| onDateChange<sup>10+</sup> | [Callback](ts-types.md#callback12)\<Date> | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。<br />**说明:**<br />当showTime设置为true时,回调接口返回值value中时和分为选择器选择的时和分。否则,返回值value中时和分为系统时间的时和分。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 57| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 58| 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开始,该接口支持在原子化服务中使用。 | 59| onDidAppear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 否 | 弹窗弹出时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onDateAccept/onCancel/onDateChange)>>onWillDisappear>>onDidDisappear。<br />2.在onDidAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br />3.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。<br />4. 当弹窗入场动效未完成时关闭弹窗,该回调不会触发。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 60| onDidDisappear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 否 | 弹窗消失时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onDateAccept/onCancel/onDateChange)>>onWillDisappear>>onDidDisappear。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 61| onWillAppear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 否 | 弹窗显示动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onDateAccept/onCancel/onDateChange)>>onWillDisappear>>onDidDisappear。<br />2.在onWillAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 62| onWillDisappear<sup>12+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 否 | 弹窗退出动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onDateAccept/onCancel/onDateChange)>>onWillDisappear>>onDidDisappear。<br />2.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 63| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明) | 否 | 设置弹窗背板的阴影。<br /> 当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 64| 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开始,该接口支持在原子化服务中使用。| 65| enableHoverMode<sup>14+</sup> | boolean | 否 | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 66| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否 | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN <br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 67 68## LunarSwitchStyle<sup>14+</sup>类型说明 69 70该类型定义了DatePickerDialog组件中切换农历开关的样式。 71 72**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 73 74**系统能力:** SystemCapability.ArkUI.ArkUI.Full 75 76| 参数名 | 类型 | 必填 | 说明 | 77| ------ |-------------------|-----|----------| 78| selectedColor| [ResourceColor](ts-types.md#resourcecolor) | 否 | 设置开关开启时开关的背景颜色。<br/>默认值:$r('sys.color.ohos_id_color_text_primary_actived')。 | 79| unselectedColor | [ResourceColor](ts-types.md#resourcecolor) | 否 | 设置开关未开启时开关的边框颜色。<br/>默认值:$r('sys.color.ohos_id_color_switch_outline_off')。 | 80| strokeColor | [ResourceColor](ts-types.md#resourcecolor) | 否 | 设置开关内部图标颜色。<br/>默认值:Color.White。 | 81 82## PickerDialogButtonStyle<sup>12+</sup>类型说明 83 84**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 85 86**系统能力:** SystemCapability.ArkUI.ArkUI.Full 87 88| 参数名 | 参数类型 | 必填 | 参数描述 | 89| ----- | ---------------------------------------- | ---- | ------------------------- | 90| type | [ButtonType](ts-basic-components-button.md#buttontype枚举说明) | 否 | 按钮显示样式。 | 91| style | [ButtonStyleMode](ts-basic-components-button.md#buttonstylemode11枚举说明) | 否 | 按钮的样式和重要程度。 | 92| role | [ButtonRole](ts-basic-components-button.md#buttonrole12枚举说明) | 否 | Button组件的角色。 | 93| fontSize | [Length](ts-types.md#length) | 否 | 文本显示字号。 | 94| fontColor | [ResourceColor](ts-types.md#resourcecolor) | 否 | 文本显示颜色。 | 95| fontWeight | [FontWeight](ts-appendix-enums.md#fontweight) \| number \| string| 否 | 文本的字体粗细,number类型取值[100, 900],取值间隔为100,取值越大,字体越粗 | 96| fontStyle | [FontStyle](ts-appendix-enums.md#fontstyle) | 否 | 文本的字体样式。 | 97| fontFamily | [Resource](ts-types.md#resource) \| string | 否 | 字体列表。默认字体'HarmonyOS Sans',当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。 | 98| backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | 否 | 按钮背景色。 | 99| borderRadius | [Length](ts-types.md#length) \| [BorderRadiuses](ts-types.md#borderradiuses9)| 否 | 圆角半径。 | 100| primary | boolean | 否 | 在弹窗获焦且未进行tab键走焦时,按钮是否默认响应Enter键。 | 101 102**异常情形说明:** 103 104| 异常情形 | 对应结果 | 105| -------- | ------------------------------------------------------------ | 106| 起始日期晚于结束日期,选中日期未设置 | 起始日期、结束日期和选中日期都为默认值 | 107| 起始日期晚于结束日期,选中日期早于起始日期默认值 | 起始日期、结束日期都为默认值,选中日期为起始日期默认值 | 108| 起始日期晚于结束日期,选中日期晚于结束日期默认值 | 起始日期、结束日期都为默认值,选中日期为结束日期默认值 | 109| 起始日期晚于结束日期,选中日期在起始日期与结束日期默认值范围内 | 起始日期、结束日期都为默认值,选中日期为设置的值 | 110| 选中日期早于起始日期 | 选中日期为起始日期 | 111| 选中日期晚于结束日期 | 选中日期为结束日期 | 112| 起始日期晚于当前系统日期,选中日期未设置 | 选中日期为起始日期 | 113| 结束日期早于当前系统日期,选中日期未设置 | 选中日期为结束日期 | 114| 日期格式不符合规范,如‘1999-13-32’ | 取默认值 | 115| 起始日期或结束日期早于系统有效范围 | 起始日期或结束日期取系统有效范围最早日期 | 116| 起始日期或结束日期晚于系统有效范围 | 起始日期或结束日期取系统有效范围最晚日期 | 117 118系统日期范围:1900-1-31 ~ 2100-12-31 119 120选中日期会在起始日期与结束日期异常处理完成后再进行异常情形判断处理 121 122## 示例 123 124> **说明:** 125> 126> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showDatePickerDialog](../js-apis-arkui-UIContext.md#showdatepickerdialog)来明确UI的执行上下文。 127 128### 示例1(弹出日期选择弹窗) 129 130该示例通过点击按钮弹出日期选择弹窗。 131 132```ts 133// xxx.ets 134@Entry 135@Component 136struct DatePickerDialogExample { 137 selectedDate: Date = new Date("2010-1-1") 138 139 build() { 140 Column() { 141 Button("DatePickerDialog") 142 .margin(20) 143 .onClick(() => { 144 DatePickerDialog.show({ // 建议使用 this.getUIContext().showDatePickerDialog()接口 145 start: new Date("2000-1-1"), 146 end: new Date("2100-12-31"), 147 selected: this.selectedDate, 148 showTime:true, 149 useMilitaryTime:false, 150 disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}}, 151 textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}}, 152 selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}}, 153 onDateAccept: (value: Date) => { 154 // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期 155 this.selectedDate = value 156 console.info("DatePickerDialog:onDateAccept()" + value.toString()) 157 }, 158 onCancel: () => { 159 console.info("DatePickerDialog:onCancel()") 160 }, 161 onDateChange: (value: Date) => { 162 console.info("DatePickerDialog:onDateChange()" + value.toString()) 163 }, 164 onDidAppear: () => { 165 console.info("DatePickerDialog:onDidAppear()") 166 }, 167 onDidDisappear: () => { 168 console.info("DatePickerDialog:onDidDisappear()") 169 }, 170 onWillAppear: () => { 171 console.info("DatePickerDialog:onWillAppear()") 172 }, 173 onWillDisappear: () => { 174 console.info("DatePickerDialog:onWillDisappear()") 175 } 176 }) 177 }) 178 179 Button("Lunar DatePickerDialog") 180 .margin(20) 181 .onClick(() => { 182 DatePickerDialog.show({ 183 start: new Date("2000-1-1"), 184 end: new Date("2100-12-31"), 185 selected: this.selectedDate, 186 lunar: true, 187 disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}}, 188 textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}}, 189 selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}}, 190 onDateAccept: (value: Date) => { 191 this.selectedDate = value 192 console.info("DatePickerDialog:onDateAccept()" + value.toString()) 193 }, 194 onCancel: () => { 195 console.info("DatePickerDialog:onCancel()") 196 }, 197 onDateChange: (value: Date) => { 198 console.info("DatePickerDialog:onDateChange()" + value.toString()) 199 }, 200 onDidAppear: () => { 201 console.info("DatePickerDialog:onDidAppear()") 202 }, 203 onDidDisappear: () => { 204 console.info("DatePickerDialog:onDidDisappear()") 205 }, 206 onWillAppear: () => { 207 console.info("DatePickerDialog:onWillAppear()") 208 }, 209 onWillDisappear: () => { 210 console.info("DatePickerDialog:onWillDisappear()") 211 } 212 }) 213 }) 214 }.width('100%') 215 } 216} 217``` 218 219 220 221### 示例2(自定义样式) 222 223该示例通过配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现了自定义文本以及按钮样式。 224 225```ts 226// xxx.ets 227@Entry 228@Component 229struct DatePickerDialogExample { 230 selectedDate: Date = new Date("2010-1-1") 231 232 build() { 233 Column() { 234 Button("DatePickerDialog") 235 .margin(20) 236 .onClick(() => { 237 DatePickerDialog.show({ 238 start: new Date("2000-1-1"), 239 end: new Date("2100-12-31"), 240 selected: this.selectedDate, 241 showTime:true, 242 useMilitaryTime:false, 243 disappearTextStyle: {color: Color.Pink, font: {size: '22fp', weight: FontWeight.Bold}}, 244 textStyle: {color: '#ff00ff00', font: {size: '18fp', weight: FontWeight.Normal}}, 245 selectedTextStyle: {color: '#ff182431', font: {size: '14fp', weight: FontWeight.Regular}}, 246 acceptButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Red, 247 fontSize: '26fp', fontWeight: FontWeight.Bolder, fontStyle: FontStyle.Normal, fontFamily: 'sans-serif', backgroundColor:'#80834511', 248 borderRadius: 20 }, 249 cancelButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Blue, 250 fontSize: '16fp', fontWeight: FontWeight.Normal, fontStyle: FontStyle.Italic, fontFamily: 'sans-serif', backgroundColor:'#50182431', 251 borderRadius: 10 }, 252 onDateAccept: (value: Date) => { 253 // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期 254 this.selectedDate = value 255 console.info("DatePickerDialog:onDateAccept()" + value.toString()) 256 }, 257 onCancel: () => { 258 console.info("DatePickerDialog:onCancel()") 259 }, 260 onDateChange: (value: Date) => { 261 console.info("DatePickerDialog:onDateChange()" + value.toString()) 262 }, 263 onDidAppear: () => { 264 console.info("DatePickerDialog:onDidAppear()") 265 }, 266 onDidDisappear: () => { 267 console.info("DatePickerDialog:onDidDisappear()") 268 }, 269 onWillAppear: () => { 270 console.info("DatePickerDialog:onWillAppear()") 271 }, 272 onWillDisappear: () => { 273 console.info("DatePickerDialog:onWillDisappear()") 274 } 275 }) 276 }) 277 }.width('100%') 278 } 279} 280``` 281 282 283 284> **说明:** 285> 286> 如需完全自定义实现日期滑动选择器弹窗,可以通过先使用[自定义弹窗 (CustomDialog)](ts-methods-custom-dialog-box.md),然后使用[DatePicker组件](ts-basic-components-datepicker.md)来实现。 287 288### 示例3(悬停态弹窗) 289 290该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。 291 292```ts 293@Entry 294@Component 295struct DatePickerDialogExample { 296 selectedDate: Date = new Date("2010-1-1"); 297 298 build() { 299 Column() { 300 Button("DatePickerDialog") 301 .margin(20) 302 .onClick(() => { 303 DatePickerDialog.show({ // 建议使用 this.getUIContext().showDatePickerDialog()接口 304 start: new Date("2000-1-1"), 305 end: new Date("2100-12-31"), 306 selected: this.selectedDate, 307 showTime: true, 308 useMilitaryTime: false, 309 disappearTextStyle: { color: Color.Pink, font: { size: '22fp', weight: FontWeight.Bold }}, 310 textStyle: { color: '#ff00ff00', font: { size: '18fp', weight: FontWeight.Normal }}, 311 selectedTextStyle: { color: '#ff182431', font: { size: '14fp', weight: FontWeight.Regular }}, 312 onDateAccept: (value: Date) => { 313 // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期 314 this.selectedDate = value; 315 console.info("DatePickerDialog:onDateAccept()" + value.toString()); 316 }, 317 onCancel: () => { 318 console.info("DatePickerDialog:onCancel()"); 319 }, 320 onDateChange: (value: Date) => { 321 console.info("DatePickerDialog:onDateChange()" + value.toString()); 322 }, 323 onDidAppear: () => { 324 console.info("DatePickerDialog:onDidAppear()"); 325 }, 326 onDidDisappear: () => { 327 console.info("DatePickerDialog:onDidDisappear()"); 328 }, 329 onWillAppear: () => { 330 console.info("DatePickerDialog:onWillAppear()"); 331 }, 332 onWillDisappear: () => { 333 console.info("DatePickerDialog:onWillDisappear()"); 334 }, 335 enableHoverMode: true, 336 hoverModeArea: HoverModeAreaType.TOP_SCREEN 337 }) 338 }) 339 }.width('100%') 340 } 341} 342``` 343 344 345