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