• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/>取值范围:大于等于0的整数。<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](ts-types.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| keyboardAvoidDistance<sup>15+</sup>       | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | 否   | 弹窗避让键盘后,和键盘之间的距离。<br />**说明:**<br />- 默认值:16vp。<br />- 默认单位:vp。<br />- 当且仅当keyboardAvoidMode属性设置为DEFAULT时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。|
63| levelMode<sup>15+</sup>       | [LevelMode](../js-apis-promptAction.md#levelmode15枚举说明) | 否   | 设置弹窗显示层级。<br />**说明:**<br />- 默认值:LevelMode.OVERLAY。<br />- 当且仅当showInSubWindow属性设置为false时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。|
64| levelUniqueId<sup>15+</sup>       | number | 否   | 设置页面级弹窗需要显示的层级下的[节点 uniqueId](../js-apis-arkui-frameNode.md#getuniqueid12)。<br />**说明:**<br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。|
65| immersiveMode<sup>15+</sup>       | [ImmersiveMode](../js-apis-promptAction.md#immersivemode15枚举说明) | 否   | 设置页面内弹窗蒙层效果。<br />**说明:**<br />- 默认值:ImmersiveMode.DEFAULT <br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。|
66
67> **说明:**
68>
69> - 按下返回键和ESC键时会让弹窗退出。
70> - 弹窗在避让软键盘时到达极限高度之后会压缩高度。
71>   需要注意:高度压缩生效在外层容器上,如果容器根节点中的子组件设置了较大的固定高度,由于容器默认不裁剪,依然可能存在超出屏幕显示的情况。
72> - 自定义弹窗仅适用于简单提示场景,不能替代页面使用。弹窗避让软键盘时,与软键盘之间存在16vp的安全间距。
73>
74> - 当前,ArkUI弹出框默认为非页面级弹出框,在页面路由跳转时,如果开发者未调用close方法将其关闭,弹出框将不会自动关闭。若需实现在跳转页面时覆盖弹出框的场景,可以使用[组件导航子页面显示类型的弹窗类型](../../../ui/arkts-navigation-navigation.md#页面显示类型)或者[页面级弹出框](../../../ui/arkts-embedded-dialog.md)。
75
76## DismissDialogAction<sup>12+</sup>
77
78Dialog关闭的信息。
79
80**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
81
82**系统能力:** SystemCapability.ArkUI.ArkUI.Full
83
84### 属性
85
86| 名称    | 类型                                                         | 可读 | 可写 | 说明                                                         |
87| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
88| dismiss | Callback&lt;void&gt;                                         | 否   | 否   | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 |
89| reason  | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否   | 否   | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 |
90
91## CustomDialogController
92
93**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
94
95### 导入对象
96
97```ts
98dialogController : CustomDialogController | null = new CustomDialogController(CustomDialogControllerOptions)
99```
100**说明**:CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效,具体用法可看下方示例。
101
102### constructor
103
104constructor(value: CustomDialogControllerOptions)
105
106自定义弹窗的构造器。
107
108**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
109
110**系统能力:** SystemCapability.ArkUI.ArkUI.Full
111
112**参数:**
113
114| 参数名 | 类型                                                         | 必填 | 说明                   |
115| ------ | ------------------------------------------------------------ | ---- | ---------------------- |
116| value  | [CustomDialogControllerOptions](#customdialogcontrolleroptions对象说明) | 是   | 配置自定义弹窗的参数。 |
117
118### open
119
120open()
121
122显示自定义弹窗内容,允许多次使用,但如果弹框为SubWindow模式,则该弹框不允许再弹出SubWindow弹框。
123
124**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
125
126**系统能力:** SystemCapability.ArkUI.ArkUI.Full
127
128
129### close
130
131close()
132
133**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
134
135**系统能力:** SystemCapability.ArkUI.ArkUI.Full
136
137
138关闭显示的自定义弹窗,若已关闭,则不生效。
139
140## 示例
141
142### 示例1(弹出嵌套弹窗)
143
144该示例实现了在CustomDialog中打开另一个或另一些CustomDialog。
145
146```ts
147// xxx.ets
148@CustomDialog
149struct CustomDialogExampleTwo {
150  controllerTwo?: CustomDialogController
151  build() {
152    Column() {
153      Text('我是第二个弹窗')
154        .fontSize(30)
155        .height(100)
156      Button('点我关闭第二个弹窗')
157        .onClick(() => {
158          if (this.controllerTwo != undefined) {
159            this.controllerTwo.close()
160          }
161        })
162        .margin(20)
163    }
164  }
165}
166@CustomDialog
167@Component
168struct CustomDialogExample {
169  @Link textValue: string
170  @Link inputValue: string
171  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
172    builder: CustomDialogExampleTwo(),
173    alignment: DialogAlignment.Bottom,
174    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
175      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
176      console.log("dialog onWillDismiss")
177      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
178        dismissDialogAction.dismiss()
179      }
180      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
181        dismissDialogAction.dismiss()
182      }
183    },
184    offset: { dx: 0, dy: -25 } })
185  controller?: CustomDialogController
186  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面
187  cancel: () => void = () => {
188  }
189  confirm: () => void = () => {
190  }
191
192  build() {
193    Column() {
194      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
195      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
196        .onChange((value: string) => {
197          this.textValue = value
198        })
199      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
200      Flex({ justifyContent: FlexAlign.SpaceAround }) {
201        Button('cancel')
202          .onClick(() => {
203            if (this.controller != undefined) {
204              this.controller.close()
205              this.cancel()
206            }
207          }).backgroundColor(0xffffff).fontColor(Color.Black)
208        Button('confirm')
209          .onClick(() => {
210            if (this.controller != undefined) {
211              this.inputValue = this.textValue
212              this.controller.close()
213              this.confirm()
214            }
215          }).backgroundColor(0xffffff).fontColor(Color.Red)
216      }.margin({ bottom: 10 })
217
218      Button('点我打开第二个弹窗')
219        .onClick(() => {
220          if (this.dialogControllerTwo != null) {
221            this.dialogControllerTwo.open()
222          }
223        })
224        .margin(20)
225    }.borderRadius(10)
226    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
227  }
228}
229@Entry
230@Component
231struct CustomDialogUser {
232  @State textValue: string = ''
233  @State inputValue: string = 'click me'
234  dialogController: CustomDialogController | null = new CustomDialogController({
235    builder: CustomDialogExample({
236      cancel: ()=> { this.onCancel() },
237      confirm: ()=> { this.onAccept() },
238      textValue: $textValue,
239      inputValue: $inputValue
240    }),
241    cancel: this.exitApp,
242    autoCancel: true,
243    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
244      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
245      console.log("dialog onWillDismiss")
246      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
247        dismissDialogAction.dismiss()
248      }
249      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
250        dismissDialogAction.dismiss()
251      }
252    },
253    alignment: DialogAlignment.Bottom,
254    offset: { dx: 0, dy: -20 },
255    gridCount: 4,
256    customStyle: false,
257    cornerRadius: 10,
258  })
259
260  // 在自定义组件即将析构销毁时将dialogController置空
261  aboutToDisappear() {
262    this.dialogController = null // 将dialogController置空
263  }
264
265  onCancel() {
266    console.info('Callback when the first button is clicked')
267  }
268
269  onAccept() {
270    console.info('Callback when the second button is clicked')
271  }
272
273  exitApp() {
274    console.info('Click the callback in the blank area')
275  }
276  build() {
277    Column() {
278      Button(this.inputValue)
279        .onClick(() => {
280          if (this.dialogController != null) {
281            this.dialogController.open()
282          }
283        }).backgroundColor(0x317aff)
284    }.width('100%').margin({ top: 5 })
285  }
286}
287```
288
289![zh-cn_image_custom](figures/zh-cn_image_custom.gif)
290
291### 示例2(可在主窗外弹出的弹窗)
292
293在2in1设备上设置showInSubWindow为true时,可以弹出在主窗外显示的弹窗。
294
295```ts
296// xxx.ets
297@CustomDialog
298struct CustomDialogExample {
299  controller?: CustomDialogController
300  cancel: () => void = () => {
301  }
302  confirm: () => void = () => {
303  }
304  build() {
305    Column() {
306      Text('可展示在主窗口外的弹窗')
307        .fontSize(30)
308        .height(100)
309      Button('点我关闭弹窗')
310        .onClick(() => {
311          if (this.controller != undefined) {
312            this.controller.close()
313          }
314        })
315        .margin(20)
316    }
317  }
318}
319@Entry
320@Component
321struct CustomDialogUser {
322  dialogController: CustomDialogController | null = new CustomDialogController({
323    builder: CustomDialogExample({
324      cancel: ()=> { this.onCancel() },
325      confirm: ()=> { this.onAccept() }
326    }),
327    cancel: this.existApp,
328    autoCancel: true,
329    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
330      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
331      console.log("dialog onWillDismiss")
332      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
333        dismissDialogAction.dismiss()
334      }
335      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
336        dismissDialogAction.dismiss()
337      }
338    },
339    alignment: DialogAlignment.Center,
340    offset: { dx: 0, dy: -20 },
341    gridCount: 4,
342    showInSubWindow: true,
343    isModal: true,
344    customStyle: false,
345    cornerRadius: 10,
346  })
347  // 在自定义组件即将析构销毁时将dialogController置空
348  aboutToDisappear() {
349    this.dialogController = null // 将dialogController置空
350  }
351
352  onCancel() {
353    console.info('Callback when the first button is clicked')
354  }
355
356  onAccept() {
357    console.info('Callback when the second button is clicked')
358  }
359
360  existApp() {
361    console.info('Click the callback in the blank area')
362  }
363
364  build() {
365    Column() {
366      Button('click me')
367        .onClick(() => {
368          if (this.dialogController != null) {
369            this.dialogController.open()
370          }
371        }).backgroundColor(0x317aff)
372    }.width('100%').margin({ top: 5 })
373  }
374}
375```
376
377![zh-cn_image_custom-showinsubwindow](figures/zh-cn_image_custom-showinsubwindow.jpg)
378
379### 示例3(设置弹窗的样式)
380该示例定义了CustomDialog的样式,如宽度、高度、背景色、阴影等等。
381```ts
382// xxx.ets
383@CustomDialog
384struct CustomDialogExample {
385  controller?: CustomDialogController
386  cancel: () => void = () => {
387  }
388  confirm: () => void = () => {
389  }
390  build() {
391    Column() {
392      Text('这是自定义弹窗')
393        .fontSize(30)
394        .height(100)
395      Button('点我关闭弹窗')
396        .onClick(() => {
397          if (this.controller != undefined) {
398            this.controller.close()
399          }
400        })
401        .margin(20)
402    }
403  }
404}
405@Entry
406@Component
407struct CustomDialogUser {
408  dialogController: CustomDialogController | null = new CustomDialogController({
409    builder: CustomDialogExample({
410      cancel: ()=> { this.onCancel() },
411      confirm: ()=> { this.onAccept() }
412    }),
413    cancel: this.existApp,
414    autoCancel: true,
415    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
416      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
417      console.log("dialog onWillDismiss")
418      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
419        dismissDialogAction.dismiss()
420      }
421      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
422        dismissDialogAction.dismiss()
423      }
424    },
425    alignment: DialogAlignment.Center,
426    offset: { dx: 0, dy: -20 },
427    customStyle: false,
428    cornerRadius: 20,
429    width: 300,
430    height: 200,
431    borderWidth: 1,
432    borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
433    borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
434    backgroundColor: Color.White,
435    shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
436  })
437  // 在自定义组件即将析构销毁时将dialogController置空
438  aboutToDisappear() {
439    this.dialogController = null // 将dialogController置空
440  }
441
442  onCancel() {
443    console.info('Callback when the first button is clicked')
444  }
445
446  onAccept() {
447    console.info('Callback when the second button is clicked')
448  }
449
450  existApp() {
451    console.info('Click the callback in the blank area')
452  }
453
454  build() {
455    Column() {
456      Button('click me')
457        .onClick(() => {
458          if (this.dialogController != null) {
459            this.dialogController.open()
460          }
461        }).backgroundColor(0x317aff)
462    }.width('100%').margin({ top: 5 })
463  }
464}
465```
466
467![zh-cn_image_custom_style](figures/zh-cn_image_custom_style.gif)
468
469### 示例4(悬停态弹窗)
470
471该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
472
473```ts
474@CustomDialog
475@Component
476struct CustomDialogExample {
477  @Link textValue: string;
478  @Link inputValue: string;
479  controller?: CustomDialogController;
480
481  build() {
482    Column() {
483      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
484      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
485        .onChange((value: string) => {
486          this.textValue = value;
487        })
488      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
489      Flex({ justifyContent: FlexAlign.SpaceAround }) {
490        Button('cancel')
491          .onClick(() => {
492            if (this.controller != undefined) {
493              this.controller.close();
494            }
495          }).backgroundColor(0xffffff).fontColor(Color.Black)
496        Button('confirm')
497          .onClick(() => {
498            if (this.controller != undefined) {
499              this.inputValue = this.textValue;
500              this.controller.close();
501            }
502          }).backgroundColor(0xffffff).fontColor(Color.Red)
503      }.margin({ bottom: 10 })
504
505      Button('点我打开第二个弹窗')
506        .margin(20)
507    }.borderRadius(10)
508    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
509  }
510}
511@Entry
512@Component
513struct CustomDialogUser {
514  @State textValue: string = '';
515  @State inputValue: string = 'click me';
516  dialogController: CustomDialogController | null = new CustomDialogController({
517    builder: CustomDialogExample({
518      textValue: $textValue,
519      inputValue: $inputValue
520    }),
521    cancel: this.exitApp,
522    autoCancel: true,
523    onWillDismiss: (dismissDialogAction: DismissDialogAction)=> {
524      console.info("reason=" + JSON.stringify(dismissDialogAction.reason));
525      console.log("dialog onWillDismiss");
526      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
527        dismissDialogAction.dismiss();
528      }
529      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
530        dismissDialogAction.dismiss();
531      }
532    },
533    alignment: DialogAlignment.Bottom,
534    offset: { dx: 0, dy: -20 },
535    gridCount: 4,
536    customStyle: false,
537    cornerRadius: 10,
538    enableHoverMode: true,
539    hoverModeArea: HoverModeAreaType.TOP_SCREEN
540  })
541
542  // 在自定义组件即将析构销毁时将dialogController置空
543  aboutToDisappear() {
544    this.dialogController = null; // 将dialogController置空
545  }
546
547  exitApp() {
548    console.info('Click the callback in the blank area');
549  }
550
551  build() {
552    Column() {
553      Button(this.inputValue)
554        .onClick(() => {
555          if (this.dialogController != null) {
556            this.dialogController.open();
557          }
558        }).backgroundColor(0x317aff)
559    }.width('100%').margin({ top: 5 })
560  }
561}
562```
563
564![zh-cn_image_custom](figures/zh-cn_image_custom_hovermode.gif)