1# 自定义弹窗 (CustomDialog)
2
3通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。
4
5> **说明:**
6>
7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 接口
11
12constructor(value: CustomDialogControllerOptions)
13
14配置自定义弹窗的参数。
15
16> **说明:**
17>
18> 自定义弹窗的所有参数,不支持动态刷新。
19
20**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
21
22**系统能力:** SystemCapability.ArkUI.ArkUI.Full
23
24**参数:**
25
26| 参数名 | 类型                                                         | 必填 | 说明                   |
27| ------ | ------------------------------------------------------------ | ---- | ---------------------- |
28| value  | [CustomDialogControllerOptions](#customdialogcontrolleroptions对象说明) | 是   | 配置自定义弹窗的参数。 |
29
30## CustomDialogControllerOptions对象说明
31
32**系统能力:** SystemCapability.ArkUI.ArkUI.Full
33
34| 名称                           | 类型                                     | 必填   | 说明                                     |
35| ----------------------------- | ---------------------------------------- | ---- | ---------------------------------------- |
36| builder                       | [CustomDialog](../../../ui/arkts-common-components-custom-dialog.md) | 是    | 自定义弹窗内容构造器。<br/>**说明:** <br/>若builder构造器使用回调函数作为入参,请注意使用this绑定问题,如build: custombuilder({ callback: ()=> {...}})。<br/>若在builder构造器中监听数据变化请使用@Link,其他方式如@Prop、@ObjectLink不适用此场景。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
37| cancel                        | ()&nbsp;=&gt;&nbsp;void                  | 否    | 返回、ESC键和点击遮障层弹窗退出时的回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
38| autoCancel                    | boolean                                  | 否    | 是否允许点击遮障层退出,true表示关闭弹窗。false表示不关闭弹窗。<br>默认值:true<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
39| alignment                     | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否    | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
40| offset                        | [Offset](ts-types.md#offset)             | 否    | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{ dx: 0, dy: 0 }<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
41| customStyle                   | boolean                                  | 否    | 弹窗容器样式是否自定义。<br>设置false时(默认值):<br/>1、圆角为32vp。<br/>2、未设置弹窗宽度高度:弹窗容器的宽度根据栅格系统自适应。高度自适应自定义的内容节点。<br/>3、设置弹窗宽度高度:弹窗容器的宽度不超过默认样式下的最大宽度(自定义节点设置100%的宽度),弹窗容器的高度不超过默认样式下的最大高度(自定义节点设置100%的高度)。<br/>设置为true:<br/>1、圆角为0,弹窗背景色为透明色。<br/>2、不支持设置弹窗宽度、高度、边框宽度、边框样式、边框颜色以及阴影宽度。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
42| gridCount<sup>8+</sup>        | number                                   | 否    | 弹窗宽度占[栅格宽度](../../../ui/arkts-layout-development-grid-layout.md)的个数。<br>默认为按照窗口大小自适应,异常值按默认值处理,最大栅格数为系统最大栅格数。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
43| maskColor<sup>10+</sup>       | [ResourceColor](ts-types.md#resourcecolor) | 否    | 自定义蒙层颜色。<br>默认值: 0x33000000<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。              |
44| maskRect<sup>10+</sup>        | [Rectangle](ts-methods-alert-dialog-box.md#rectangle8类型说明) | 否     | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。<br/>默认值:{ x: 0, y: 0, width: '100%', height: '100%' } <br/>**说明:**<br/>showInSubWindow为true时,maskRect不生效。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
45| openAnimation<sup>10+</sup>   | [AnimateParam](ts-explicit-animation.md#animateparam对象说明) | 否    | 自定义设置弹窗弹出的动画效果相关参数。<br>**说明**:<br>tempo默认值为1,当设置小于等于0的值时按默认值处理。<br/>iterations默认值为1,默认播放一次,设置为其他数值时按默认值处理。<br>playMode控制动画播放模式,默认值为PlayMode.Normal,设置为其他数值时按照默认值处理。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
46| closeAnimation<sup>10+</sup>  | [AnimateParam](ts-explicit-animation.md#animateparam对象说明) | 否    | 自定义设置弹窗关闭的动画效果相关参数。<br>**说明**:<br>tempo默认值为1,当设置小于等于0的值时按默认值处理。<br/>iterations默认值为1,默认播放一次,设置为其他数值时按默认值处理。<br>playMode控制动画播放模式,默认值为PlayMode.Normal,设置为其他数值时按照默认值处理。<br/>页面转场切换时,建议使用默认关闭动效。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
47| showInSubWindow<sup>10+</sup> | boolean                                  | 否    | 某弹框需要显示在主窗口之外时,是否在子窗口显示此弹窗。<br>默认值:false,弹窗显示在应用内,而非独立子窗口。<br>**说明**:showInSubWindow为true的弹窗无法触发显示另一个showInSubWindow为true的弹窗。不建议在showInSubWindow为true的弹窗中使用CalendarPicker、CalendarPickerDialog、DatePickerDialog、TextPickerDialog、TimePickerDialog组件,弹窗会影响上述组件行为。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
48| backgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor)      | 否   | 设置弹窗背板填充。<br/>默认值:Color.Transparent<br />**说明:** 如果同时设置了内容构造器的背景色,则backgroundColor会被内容构造器的背景色覆盖。<br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
49| cornerRadius<sup>10+</sup>    | [Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[BorderRadiuses](ts-types.md#borderradiuses9) | 否   | 设置背板的圆角半径。<br />可分别设置4个圆角的半径。<br />默认值:{ topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }<br />**说明**:自定义弹窗默认的背板圆角半径为32vp,如果需要使用cornerRadius属性,请和[borderRadius](ts-universal-attributes-border.md#borderradius)属性一起使用。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
50| isModal<sup>11+</sup> | boolean | 否 | 弹窗是否为模态窗口,模态窗口有蒙层,非模态窗口无蒙层。<br/>默认值:true,此时弹窗有蒙层。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
51| onWillDismiss<sup>12+</sup> | Callback<[DismissDialogAction](#dismissdialogaction12)> | 否 | 交互式关闭回调函数。<br/>**说明:**<br/>1.当用户执行点击遮障层关闭、左滑/右滑、三键back、键盘ESC关闭交互操作时,如果注册该回调函数,则不会立刻关闭弹窗。在回调函数中可以通过reason得到阻拦关闭弹窗的操作类型,从而根据原因选择是否能关闭弹窗。当前组件返回的reason中,暂不支持CLOSE_BUTTON的枚举值。<br/>2.在onWillDismiss回调中,不能再做onWillDismiss拦截。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
52| borderWidth<sup>12+</sup> | [Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[EdgeWidths](ts-types.md#edgewidths9)  | 否 | 设置弹窗背板的边框宽度。<br />可分别设置4个边框宽度。<br />默认值:0。<br /> 百分比参数方式:以父元素弹窗宽的百分比来设置弹窗的边框宽度。<br />当弹窗左边框和右边框大于弹窗宽度,弹窗上边框和下边框大于弹窗高度,显示可能不符合预期。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
53| borderColor<sup>12+</sup> | [ResourceColor](ts-types.md#resourcecolor)&nbsp;\|&nbsp;[EdgeColors](ts-types.md#edgecolors9)  | 否 | 设置弹窗背板的边框颜色。<br/>默认值:Color.Black<br/>如果使用borderColor属性,需要和borderWidth属性一起使用。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
54| borderStyle<sup>12+</sup> | [BorderStyle](ts-appendix-enums.md#borderstyle)&nbsp;\|&nbsp;[EdgeStyles](ts-types.md#edgestyles9)  | 否 | 设置弹窗背板的边框样式。<br/>默认值:BorderStyle.Solid<br/>如果使用borderStyle属性,需要和borderWidth属性一起使用。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
55| width<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | 否   | 设置弹窗背板的宽度。<br />**说明:**<br>- 弹窗宽度默认最大值:400vp。<br />- 百分比参数方式:弹窗参考宽度为所在窗口的宽度,在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
56| height<sup>12+</sup> | [Dimension](ts-types.md#dimension10)   | 否 | 设置弹窗背板的高度。<br />**说明:**<br />- 弹窗高度默认最大值:0.9 *(窗口高度 - 安全区域)。<br />- 百分比参数方式:弹窗参考高度为(窗口高度 - 安全区域),在此基础上调小或调大。<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| backgroundBlurStyle<sup>12+</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| keyboardAvoidMode<sup>12+</sup> | [KeyboardAvoidMode](../js-apis-promptAction.md#keyboardavoidmode12枚举说明) | 否 | 用于设置弹窗是否在拉起软键盘时进行自动避让。<br/>默认值:KeyboardAvoidMode.DEFAULT<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
60| enableHoverMode<sup>14+</sup>     | boolean | 否   | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
61| hoverModeArea<sup>14+</sup>       | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否   | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
62
63> **说明:**
64>
65> - 按下返回键和ESC键时会让弹窗退出。
66> - 弹窗在避让软键盘时到达极限高度之后会压缩高度。
67>   需要注意:高度压缩生效在外层容器上,如果容器根节点中的子组件设置了较大的固定高度,由于容器默认不裁剪,依然可能存在超出屏幕显示的情况。
68> - 自定义弹窗仅适用于简单提示场景,不能替代页面使用。弹窗避让软键盘时,与软键盘之间存在16vp的安全间距。
69>
70> - 当前,ArkUI弹窗均为非页面级弹窗,在页面路由跳转时,如果开发者未调用close方法将其关闭,弹窗将不会自动关闭。若需实现在跳转页面时覆盖弹窗的场景,建议使用Navigation。具体使用方法,请参考[组件导航子页面显示类型的弹窗类型](../../../ui/arkts-navigation-navigation.md#页面显示类型)。
71
72## DismissDialogAction<sup>12+</sup>
73
74Dialog关闭的信息。
75
76**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
77
78**系统能力:** SystemCapability.ArkUI.ArkUI.Full
79
80### 属性
81
82| 名称    | 类型                                                         | 可读 | 可写 | 说明                                                         |
83| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
84| dismiss | Callback&lt;void&gt;                                         | 否   | 否   | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 |
85| reason  | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否   | 否   | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 |
86
87## CustomDialogController
88
89**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
90
91### 导入对象
92
93```ts
94dialogController : CustomDialogController | null = new CustomDialogController(CustomDialogControllerOptions)
95```
96**说明**:CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,具体用法可看下方示例。
97
98### constructor
99
100constructor(value: CustomDialogControllerOptions)
101
102自定义弹窗的构造器。
103
104**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
105
106**系统能力:** SystemCapability.ArkUI.ArkUI.Full
107
108**参数:**
109
110| 参数名 | 类型                                                         | 必填 | 说明                   |
111| ------ | ------------------------------------------------------------ | ---- | ---------------------- |
112| value  | [CustomDialogControllerOptions](#customdialogcontrolleroptions对象说明) | 是   | 配置自定义弹窗的参数。 |
113
114### open
115
116open()
117
118显示自定义弹窗内容,允许多次使用,但如果弹框为SubWindow模式,则该弹框不允许再弹出SubWindow弹框。
119
120**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
121
122**系统能力:** SystemCapability.ArkUI.ArkUI.Full
123
124
125### close
126
127close()
128
129**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
130
131**系统能力:** SystemCapability.ArkUI.ArkUI.Full
132
133
134关闭显示的自定义弹窗,若已关闭,则不生效。
135
136## 示例
137
138### 示例1(弹出嵌套弹窗)
139
140该示例实现了在CustomDialog中打开另一个或另一些CustomDialog。
141
142```ts
143// xxx.ets
144@CustomDialog
145struct CustomDialogExampleTwo {
146  controllerTwo?: CustomDialogController
147  build() {
148    Column() {
149      Text('我是第二个弹窗')
150        .fontSize(30)
151        .height(100)
152      Button('点我关闭第二个弹窗')
153        .onClick(() => {
154          if (this.controllerTwo != undefined) {
155            this.controllerTwo.close()
156          }
157        })
158        .margin(20)
159    }
160  }
161}
162@CustomDialog
163@Component
164struct CustomDialogExample {
165  @Link textValue: string
166  @Link inputValue: string
167  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
168    builder: CustomDialogExampleTwo(),
169    alignment: DialogAlignment.Bottom,
170    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
171      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
172      console.log("dialog onWillDismiss")
173      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
174        dismissDialogAction.dismiss()
175      }
176      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
177        dismissDialogAction.dismiss()
178      }
179    },
180    offset: { dx: 0, dy: -25 } })
181  controller?: CustomDialogController
182  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面
183  cancel: () => void = () => {
184  }
185  confirm: () => void = () => {
186  }
187
188  build() {
189    Column() {
190      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
191      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
192        .onChange((value: string) => {
193          this.textValue = value
194        })
195      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
196      Flex({ justifyContent: FlexAlign.SpaceAround }) {
197        Button('cancel')
198          .onClick(() => {
199            if (this.controller != undefined) {
200              this.controller.close()
201              this.cancel()
202            }
203          }).backgroundColor(0xffffff).fontColor(Color.Black)
204        Button('confirm')
205          .onClick(() => {
206            if (this.controller != undefined) {
207              this.inputValue = this.textValue
208              this.controller.close()
209              this.confirm()
210            }
211          }).backgroundColor(0xffffff).fontColor(Color.Red)
212      }.margin({ bottom: 10 })
213
214      Button('点我打开第二个弹窗')
215        .onClick(() => {
216          if (this.dialogControllerTwo != null) {
217            this.dialogControllerTwo.open()
218          }
219        })
220        .margin(20)
221    }.borderRadius(10)
222    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
223  }
224}
225@Entry
226@Component
227struct CustomDialogUser {
228  @State textValue: string = ''
229  @State inputValue: string = 'click me'
230  dialogController: CustomDialogController | null = new CustomDialogController({
231    builder: CustomDialogExample({
232      cancel: ()=> { this.onCancel() },
233      confirm: ()=> { this.onAccept() },
234      textValue: $textValue,
235      inputValue: $inputValue
236    }),
237    cancel: this.exitApp,
238    autoCancel: true,
239    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
240      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
241      console.log("dialog onWillDismiss")
242      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
243        dismissDialogAction.dismiss()
244      }
245      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
246        dismissDialogAction.dismiss()
247      }
248    },
249    alignment: DialogAlignment.Bottom,
250    offset: { dx: 0, dy: -20 },
251    gridCount: 4,
252    customStyle: false,
253    cornerRadius: 10,
254  })
255
256  // 在自定义组件即将析构销毁时将dialogController置空
257  aboutToDisappear() {
258    this.dialogController = null // 将dialogController置空
259  }
260
261  onCancel() {
262    console.info('Callback when the first button is clicked')
263  }
264
265  onAccept() {
266    console.info('Callback when the second button is clicked')
267  }
268
269  exitApp() {
270    console.info('Click the callback in the blank area')
271  }
272  build() {
273    Column() {
274      Button(this.inputValue)
275        .onClick(() => {
276          if (this.dialogController != null) {
277            this.dialogController.open()
278          }
279        }).backgroundColor(0x317aff)
280    }.width('100%').margin({ top: 5 })
281  }
282}
283```
284
285![zh-cn_image_custom](figures/zh-cn_image_custom.gif)
286
287### 示例2(可在主窗外弹出的弹窗)
288
289该示例通过配置showInSubWindow为true弹出可以在主窗外显示的弹窗。
290
291```ts
292// xxx.ets
293@CustomDialog
294struct CustomDialogExample {
295  controller?: CustomDialogController
296  cancel: () => void = () => {
297  }
298  confirm: () => void = () => {
299  }
300  build() {
301    Column() {
302      Text('可展示在主窗口外的弹窗')
303        .fontSize(30)
304        .height(100)
305      Button('点我关闭弹窗')
306        .onClick(() => {
307          if (this.controller != undefined) {
308            this.controller.close()
309          }
310        })
311        .margin(20)
312    }
313  }
314}
315@Entry
316@Component
317struct CustomDialogUser {
318  dialogController: CustomDialogController | null = new CustomDialogController({
319    builder: CustomDialogExample({
320      cancel: ()=> { this.onCancel() },
321      confirm: ()=> { this.onAccept() }
322    }),
323    cancel: this.existApp,
324    autoCancel: true,
325    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
326      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
327      console.log("dialog onWillDismiss")
328      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
329        dismissDialogAction.dismiss()
330      }
331      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
332        dismissDialogAction.dismiss()
333      }
334    },
335    alignment: DialogAlignment.Center,
336    offset: { dx: 0, dy: -20 },
337    gridCount: 4,
338    showInSubWindow: true,
339    isModal: true,
340    customStyle: false,
341    cornerRadius: 10,
342  })
343  // 在自定义组件即将析构销毁时将dialogController置空
344  aboutToDisappear() {
345    this.dialogController = null // 将dialogController置空
346  }
347
348  onCancel() {
349    console.info('Callback when the first button is clicked')
350  }
351
352  onAccept() {
353    console.info('Callback when the second button is clicked')
354  }
355
356  existApp() {
357    console.info('Click the callback in the blank area')
358  }
359
360  build() {
361    Column() {
362      Button('click me')
363        .onClick(() => {
364          if (this.dialogController != null) {
365            this.dialogController.open()
366          }
367        }).backgroundColor(0x317aff)
368    }.width('100%').margin({ top: 5 })
369  }
370}
371```
372
373![zh-cn_image_custom-showinsubwindow](figures/zh-cn_image_custom-showinsubwindow.jpg)
374
375### 示例3(设置弹窗的样式)
376该示例定义了CustomDialog的样式,如宽度、高度、背景色、阴影等等。
377```ts
378// xxx.ets
379@CustomDialog
380struct CustomDialogExample {
381  controller?: CustomDialogController
382  cancel: () => void = () => {
383  }
384  confirm: () => void = () => {
385  }
386  build() {
387    Column() {
388      Text('这是自定义弹窗')
389        .fontSize(30)
390        .height(100)
391      Button('点我关闭弹窗')
392        .onClick(() => {
393          if (this.controller != undefined) {
394            this.controller.close()
395          }
396        })
397        .margin(20)
398    }
399  }
400}
401@Entry
402@Component
403struct CustomDialogUser {
404  dialogController: CustomDialogController | null = new CustomDialogController({
405    builder: CustomDialogExample({
406      cancel: ()=> { this.onCancel() },
407      confirm: ()=> { this.onAccept() }
408    }),
409    cancel: this.existApp,
410    autoCancel: true,
411    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
412      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
413      console.log("dialog onWillDismiss")
414      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
415        dismissDialogAction.dismiss()
416      }
417      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
418        dismissDialogAction.dismiss()
419      }
420    },
421    alignment: DialogAlignment.Center,
422    offset: { dx: 0, dy: -20 },
423    customStyle: false,
424    cornerRadius: 20,
425    width: 300,
426    height: 200,
427    borderWidth: 1,
428    borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
429    borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
430    backgroundColor: Color.White,
431    shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
432  })
433  // 在自定义组件即将析构销毁时将dialogController置空
434  aboutToDisappear() {
435    this.dialogController = null // 将dialogController置空
436  }
437
438  onCancel() {
439    console.info('Callback when the first button is clicked')
440  }
441
442  onAccept() {
443    console.info('Callback when the second button is clicked')
444  }
445
446  existApp() {
447    console.info('Click the callback in the blank area')
448  }
449
450  build() {
451    Column() {
452      Button('click me')
453        .onClick(() => {
454          if (this.dialogController != null) {
455            this.dialogController.open()
456          }
457        }).backgroundColor(0x317aff)
458    }.width('100%').margin({ top: 5 })
459  }
460}
461```
462
463![zh-cn_image_custom_style](figures/zh-cn_image_custom_style.gif)
464
465### 示例4(悬停态弹窗)
466
467该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
468
469```ts
470@CustomDialog
471@Component
472struct CustomDialogExample {
473  @Link textValue: string;
474  @Link inputValue: string;
475  controller?: CustomDialogController;
476
477  build() {
478    Column() {
479      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
480      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
481        .onChange((value: string) => {
482          this.textValue = value;
483        })
484      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
485      Flex({ justifyContent: FlexAlign.SpaceAround }) {
486        Button('cancel')
487          .onClick(() => {
488            if (this.controller != undefined) {
489              this.controller.close();
490            }
491          }).backgroundColor(0xffffff).fontColor(Color.Black)
492        Button('confirm')
493          .onClick(() => {
494            if (this.controller != undefined) {
495              this.inputValue = this.textValue;
496              this.controller.close();
497            }
498          }).backgroundColor(0xffffff).fontColor(Color.Red)
499      }.margin({ bottom: 10 })
500
501      Button('点我打开第二个弹窗')
502        .margin(20)
503    }.borderRadius(10)
504    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
505  }
506}
507@Entry
508@Component
509struct CustomDialogUser {
510  @State textValue: string = '';
511  @State inputValue: string = 'click me';
512  dialogController: CustomDialogController | null = new CustomDialogController({
513    builder: CustomDialogExample({
514      textValue: $textValue,
515      inputValue: $inputValue
516    }),
517    cancel: this.exitApp,
518    autoCancel: true,
519    onWillDismiss: (dismissDialogAction: DismissDialogAction)=> {
520      console.info("reason=" + JSON.stringify(dismissDialogAction.reason));
521      console.log("dialog onWillDismiss");
522      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
523        dismissDialogAction.dismiss();
524      }
525      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
526        dismissDialogAction.dismiss();
527      }
528    },
529    alignment: DialogAlignment.Bottom,
530    offset: { dx: 0, dy: -20 },
531    gridCount: 4,
532    customStyle: false,
533    cornerRadius: 10,
534    enableHoverMode: true,
535    hoverModeArea: HoverModeAreaType.TOP_SCREEN
536  })
537
538  // 在自定义组件即将析构销毁时将dialogController置空
539  aboutToDisappear() {
540    this.dialogController = null; // 将dialogController置空
541  }
542
543  exitApp() {
544    console.info('Click the callback in the blank area');
545  }
546
547  build() {
548    Column() {
549      Button(this.inputValue)
550        .onClick(() => {
551          if (this.dialogController != null) {
552            this.dialogController.open();
553          }
554        }).backgroundColor(0x317aff)
555    }.width('100%').margin({ top: 5 })
556  }
557}
558```
559
560![zh-cn_image_custom](figures/zh-cn_image_custom_hovermode.gif)