1# 警告弹窗 (AlertDialog)
2
3显示警告弹窗组件,可设置文本内容与响应回调。
4
5>  **说明:**
6>
7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。
10>
11> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)来明确UI的执行上下文。
12
13## AlertDialog
14
15### show
16
17static show(value: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions)
18
19定义警告弹窗并弹出。
20
21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
22
23**系统能力:** SystemCapability.ArkUI.ArkUI.Full
24
25**参数:**
26
27| 参数名   | 类型  | 必填 | 说明 |
28| ---- | --------------- | -------- | -------- |
29| value | [AlertDialogParamWithConfirm](#alertdialogparamwithconfirm对象说明)&nbsp;\|&nbsp;[AlertDialogParamWithButtons](#alertdialogparamwithbuttons对象说明)&nbsp;\|&nbsp;[AlertDialogParamWithOptions](#alertdialogparamwithoptions10对象说明)<sup>10+</sup> | 是 | 定义并显示AlertDialog组件。 |
30
31## AlertDialogParam对象说明
32
33**系统能力:** SystemCapability.ArkUI.ArkUI.Full
34
35| 名称                              | 类型                                                         | 必填 | 说明                                                        |
36| --------------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
37| title                             | [ResourceStr](ts-types.md#resourcestr)                       | 否   | 弹窗标题。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                  |
38| subtitle<sup>10+</sup>            | [ResourceStr](ts-types.md#resourcestr)                       | 否   | 弹窗副标题。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                 |
39| message                           | [ResourceStr](ts-types.md#resourcestr)                       | 是   | 弹窗内容。  <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                 |
40| autoCancel                        | boolean                                                      | 否   | 点击遮障层时,是否关闭弹窗,true表示关闭弹窗。false表示不关闭弹窗。<br/>默认值:true<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
41| cancel                            | ()&nbsp;=&gt;&nbsp;void | 否   | 点击遮障层关闭dialog时的回调。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                               |
42| alignment                         | [DialogAlignment](#dialogalignment枚举说明)                  | 否   | 弹窗在竖直方向上的对齐方式。<br/>默认值:DialogAlignment.Default <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。<br/>**说明**:<br/>若在UIExtension中设置showInSubWindow为true, 弹窗将基于UIExtension的宿主窗口对齐。|
43| offset                            | [Offset](ts-types.md#offset)                                 | 否   | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{&nbsp;dx:&nbsp;0&nbsp;,&nbsp;dy:&nbsp;0&nbsp;}<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
44| gridCount                         | number                                                       | 否   | 弹窗容器宽度所占用栅格数。<br/>默认值:4 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                    |
45| maskRect<sup>10+</sup>            | [Rectangle](#rectangle8类型说明)                             | 否   | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。<br/>默认值:{ x: 0, y: 0, width: '100%', height: '100%' } <br/>**说明:**<br/>showInSubWindow为true时,maskRect不生效。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
46| showInSubWindow<sup>11+</sup>     | boolean                                                      | 否   | 某弹框需要显示在主窗口之外时,是否在子窗口显示此弹窗。<br/>默认值:false,弹窗显示在应用内,而非独立子窗口。<br/>**说明**:showInSubWindow为true的弹窗无法触发显示另一个showInSubWindow为true的弹窗。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
47| isModal<sup>11+</sup>             | boolean                                                      | 否   | 弹窗是否为模态窗口,模态窗口有蒙层,非模态窗口无蒙层。<br/>默认值:true,此时弹窗有蒙层。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
48| backgroundColor<sup>11+</sup>     | [ResourceColor](ts-types.md#resourcecolor)                   | 否   | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
49| 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开始,该接口支持在原子化服务中使用。 |
50| 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开始,该接口支持在原子化服务中使用。|
51| cornerRadius<sup>12+</sup>        | &nbsp;[Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[BorderRadiuses](ts-types.md#borderradiuses9)&nbsp;\|&nbsp;[LocalizedBorderRadiuses](ts-types.md#LocalizedBorderRadiuses12) | 否   | 设置背板的圆角半径。<br />可分别设置4个圆角的半径。<br />默认值:{ topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }<br /> 圆角大小受组件尺寸限制,最大值为组件宽或高的一半,若值为负,则按照默认值处理。 <br /> 百分比参数方式:以父元素弹窗宽和高的百分比来设置弹窗的圆角。<br/>**说明:**<br/>当cornerRadius属性类型为LocalizedBorderRadiuses时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
52| transition<sup>12+</sup>          | [TransitionEffect](ts-transition-animation-component.md#transitioneffect10) | 否   | 设置弹窗显示和退出的过渡效果。<br/>**说明:**<br/> 1.如果不设置,则使用默认的显示/退出动效。<br/> 2.显示动效中按back键,打断显示动效,执行退出动效,动画效果为显示动效与退出动效的曲线叠加后的效果。<br/> 3.退出动效中按back键,不会打断退出动效,退出动效继续执行,继续按back键退出应用。                               <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
53| width<sup>12+</sup>               | [Dimension](ts-types.md#dimension10) | 否   | 设置弹窗背板的宽度。<br />**说明:**<br>- 弹窗宽度默认最大值:400vp。<br />- 百分比参数方式:弹窗参考宽度为所在窗口的宽度,在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
54| height<sup>12+</sup>              | [Dimension](ts-types.md#dimension10)                         | 否   | 设置弹窗背板的高度。<br />**说明:**<br />- 弹窗高度默认最大值:0.9 *(窗口高度 - 安全区域)。<br />- 百分比参数方式:弹窗参考高度为(窗口高度 - 安全区域),在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
55| borderWidth<sup>12+</sup>         | [Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[EdgeWidths](ts-types.md#edgewidths9)&nbsp;\|&nbsp;[LocalizedEdgeWidths](ts-types.md#LocalizedEdgeWidths12) | 否   | <br />可分别设置4个边框宽度。<br />默认值:0<br /> 百分比参数方式:以父元素弹窗宽的百分比来设置弹窗的边框宽度。<br />当弹窗左边框和右边框大于弹窗宽度,弹窗上边框和下边框大于弹窗高度,显示可能不符合预期。<br/>**说明:**<br/>当borderWidth属性类型为LocalizedEdgeWidths时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
56| borderColor<sup>12+</sup>         | [ResourceColor](ts-types.md#resourcecolor)&nbsp;\|&nbsp;[EdgeColors](ts-types.md#edgecolors9)&nbsp;\|&nbsp;[LocalizedEdgeColors](ts-types.md#LocalizedEdgeColors12) | 否   | 设置弹窗背板的边框颜色。<br/>默认值:Color.Black<br/> 如果使用borderColor属性,需要和borderWidth属性一起使用。**说明:**<br/>当borderColor属性类型为LocalizedEdgeColors时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
57| 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开始,该接口支持在原子化服务中使用。 |
58| 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开始,该接口支持在原子化服务中使用。|
59| textStyle<sup>12+</sup>              | [TextStyle](#textstyle12对象说明) | 否   | 设置弹窗message内容的文本样式。 <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## AlertDialogParamWithConfirm对象说明
64
65继承自[AlertDialogParam](#alertdialogparam对象说明)。
66
67**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
68
69**系统能力:** SystemCapability.ArkUI.ArkUI.Full
70
71| 名称       | 类型     | 必填     | 说明         |
72| ---------- | ---------------- | ---------- | ------------------------------- |
73| confirm    | {<br/>enabled<sup>10+</sup>?: boolean,<br/>defaultFocus<sup>10+</sup>?: boolean,<br />style<sup>10+</sup>?: [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10),<br />value:&nbsp;[ResourceStr](ts-types.md#resourcestr),<br/>fontColor?:&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>backgroundColor?:&nbsp;&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void<br/>} | 否   | 确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。<br />enabled:点击Button是否响应,true表示Button可以响应,false表示Button不可以响应。<br />默认值:true<br />defaultFocus:设置Button是否是默认焦点,true表示Button是默认焦点,false表示Button不是默认焦点。<br />默认值:false<br />style:设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT。<br />value:Button文本内容。<br />fontColor:Button文本颜色。<br />backgroundColor:Button背景颜色。<br />action:&nbsp;Button选中时的回调。 |
74
75confirm参数优先级:fontColor、backgroundColor  > style > defaultFocus
76
77| backgroundColor | fontColor | style                       | defaultFocus | 效果     |
78| --------------- | --------- | --------------------------- | ------------ | -------- |
79| 绿底            | 红字      | -                           | -            | 绿底红字 |
80| 绿底            | -         | DialogButtonStyle.HIGHLIGHT | -            | 绿底白字 |
81| 绿底            | -         | DialogButtonStyle.DEFAULT   | -            | 绿底蓝字 |
82| 绿底            | -         | -                           | TRUE         | 绿底白字 |
83| 绿底            | -         | -                           | FALSE/-      | 绿底蓝字 |
84| -               | 红字      | DialogButtonStyle.HIGHLIGHT | -            | 蓝底红字 |
85| -               | 红字      | DialogButtonStyle.DEFAULT   | -            | 白底红字 |
86| -               | 红字      | -                           | TRUE         | 蓝底红字 |
87| -               | 红字      | -                           | FALSE/-      | 白底红字 |
88| -               | -         | DialogButtonStyle.HIGHLIGHT | -            | 蓝底白字 |
89| -               | -         | DialogButtonStyle.DEFAULT   | -            | 白底蓝字 |
90| -               | -         | -                           | TRUE         | 蓝底白字 |
91| -               | -         | -                           | FALSE/-      | 白底蓝字 |
92
93## AlertDialogParamWithButtons对象说明
94
95继承自[AlertDialogParam](#alertdialogparam对象说明)。
96
97**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
98
99**系统能力:** SystemCapability.ArkUI.ArkUI.Full
100
101| 名称             | 类型                | 必填     | 说明                     |
102| --------------- | ---------------------- | ------------ | --------------------- |
103| primaryButton   | {<br/>enabled<sup>10+</sup>?: boolean,<br/>defaultFocus<sup>10+</sup>?: boolean,<br />style<sup>10+</sup>?: [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10),<br />value:&nbsp;[ResourceStr](ts-types.md#resourcestr),<br/>fontColor?:&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>backgroundColor?:&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void;<br/>} | 是 | 确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。<br />enabled:点击Button是否响应。<br />默认值:true<br />defaultFocus:设置Button是否是默认焦点。<br />默认值:false<br />style:设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT。<br />value:Button文本内容。<br />fontColor:Button文本颜色。<br />backgroundColor:Button背景颜色。<br />action:&nbsp;Button选中时的回调。 |
104| secondaryButton | {<br/>enabled<sup>10+</sup>?: boolean,<br/>defaultFocus<sup>10+</sup>?: boolean,<br />style<sup>10+</sup>?: [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10),<br />value:&nbsp;[ResourceStr](ts-types.md#resourcestr),<br/>fontColor?:&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>backgroundColor?:&nbsp;[ResourceColor](ts-types.md#resourcecolor),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void;<br/>} | 是 | 确认Button的使能状态、默认焦点、按钮风格、文本内容、文本色、按钮背景色和点击回调。<br />enabled:点击Button是否响应。<br />默认值:true。<br />defaultFocus:设置Button是否是默认焦点。<br />默认值:false。<br />style:设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT。<br />value:Button文本内容。<br />fontColor:Button文本颜色。<br />backgroundColor:Button背景颜色。<br />action:&nbsp;Button选中时的回调。 |
105
106## AlertDialogParamWithOptions<sup>10+</sup>对象说明
107
108继承自[AlertDialogParam](#alertdialogparam对象说明)。
109
110**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
111
112**系统能力:** SystemCapability.ArkUI.ArkUI.Full
113
114| 名称             | 类型                | 必填     | 说明                    |
115| --------------- | ---------------------- | ------------ | --------------------- |
116| buttons       | Array&lt;[AlertDialogButtonOptions](#alertdialogbuttonoptions10对象说明)&gt;                 | 是  | 弹窗容器中的多个按钮。 |
117|buttonDirection      | [DialogButtonDirection](#dialogbuttondirection10枚举说明)| 否  | 按钮排布方向默认值为DialogButtonDirection.AUTO,建议3个以上按钮使用Auto模式(两个以上按钮会切换为纵向模式,通常能显示更多按钮),非Auto模式下,3个以上按钮可能会显示不全,超出显示范围的按钮会被截断。|
118
119## AlertDialogButtonOptions<sup>10+</sup>对象说明
120
121**系统能力:** SystemCapability.ArkUI.ArkUI.Full
122
123| 名称             | 类型                | 必填     | 说明                    |
124| ------------------| ---------------------- | ------------ | --------------------- |
125| enabled           | boolean | 否     | 点击Button是否响应,默认值true。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
126| defaultFocus           | boolean | 否     | 设置Button是否是默认焦点,默认值false。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
127| style           | [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10) | 否     | 设置Button的风格样式,默认值DialogButtonStyle.DEFAULT。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
128| value           | [ResourceStr](ts-types.md#resourcestr) | 是     | Button的文本内容,若值为null,则该按钮不显示。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
129| fontColor           | [ResourceColor](ts-types.md#resourcecolor) | 否     | Button的文本颜色。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
130| backgroundColor           | [ResourceColor](ts-types.md#resourcecolor) | 否     | Button背景颜色。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
131| action           | 	() => void | 是     | Button选中时的回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
132| primary<sup>12+</sup> |	boolean | 否    | 在弹窗获焦且未进行tab键走焦时,按钮是否默认响应Enter键。多个Button时,只允许一个Button的该字段配置为true,否则所有Button均不响应。多重弹窗可自动获焦连续响应。在defaultFocus为true时不生效。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
133
134## DialogButtonDirection<sup>10+</sup>枚举说明
135
136**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
137
138**系统能力:** SystemCapability.ArkUI.ArkUI.Full
139
140| 名称                       | 说明    |
141| -------------------------- | --------- |
142| AUTO                      | 两个及以下按钮水平排布,两个以上为竖直排布。 |
143| HORIZONTAL                      | 按钮水平布局。 |
144| VERTICAL                      | 按钮竖直布局。 |
145
146## DialogAlignment枚举说明
147
148**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
149
150**系统能力:** SystemCapability.ArkUI.ArkUI.Full
151
152| 名称                     | 说明           |
153| ------------------------ | -------------- |
154| Top                      | 垂直顶部对齐。 |
155| Center                   | 垂直居中对齐。 |
156| Bottom                   | 垂直底部对齐。 |
157| Default                  | 默认对齐。     |
158| TopStart<sup>8+</sup>    | 左上对齐。     |
159| TopEnd<sup>8+</sup>      | 右上对齐。     |
160| CenterStart<sup>8+</sup> | 左中对齐。     |
161| CenterEnd<sup>8+</sup>   | 右中对齐。     |
162| BottomStart<sup>8+</sup> | 左下对齐。     |
163| BottomEnd<sup>8+</sup>   | 右下对齐。     |
164
165## Rectangle<sup>8+</sup>类型说明
166
167Rectangle是各种Dialog中maskRect参数的类型。
168
169**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
170
171**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
172
173**系统能力:** SystemCapability.ArkUI.ArkUI.Full
174
175| 名称     | 类型                           | 必填 | 说明                               |
176|--------|------------------------------|----|-----------------------------------|
177| x      | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域相对于窗口左上角的x轴坐标。<br/>默认值:0vp |
178| y      | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域相对于窗口左上角的y轴坐标。<br/>默认值:0vp |
179| width  | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域的宽度。<br/>默认值:'100%'        |
180| height | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域的高度。<br/>默认值:'100%'        |
181
182>  **说明:**
183>
184>  x和y可以设置正负值百分比。当x设置为'100%'时表示遮蔽层区域往右偏移窗口本身宽度大小,当x设置为'-100%'时表示遮蔽层区域往左偏移窗口本身宽度大小。当y设置为'100%'时表示遮蔽层区域往下偏移窗口本身高度大小,当y设置为'-100%'时表示遮蔽层区域往上偏移窗口本身高度大小。
185>
186>  width和height只能设置正值,支持百分比,如果设置为负值,那么该值将被重置为默认值。
187>
188>  百分比相对于窗口自身宽高进行计算。
189
190## DismissDialogAction<sup>12+</sup>
191
192Dialog关闭的信息。
193
194**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
195
196**系统能力:** SystemCapability.ArkUI.ArkUI.Full
197
198### 属性
199
200| 名称    | 类型                                                         | 可读 | 可写 | 说明                                                         |
201| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
202| dismiss | Callback&lt;void&gt;                                         | 否   | 否   | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 |
203| reason  | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否   | 否   | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 |
204
205## TextStyle<sup>12+</sup>对象说明
206
207**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
208
209**系统能力:** SystemCapability.ArkUI.ArkUI.Full
210
211| 名称     | 类型                           | 必填 | 说明                                |
212|--------|------------------------------|----|-----------------------------------|
213| wordBreak      | [WordBreak](ts-appendix-enums.md#wordbreak11) | 否  | 弹窗message内容的文本截断方式。<br/>默认值:WordBreak.BREAK_ALL |
214
215## 示例
216
217### 示例1(弹出多个按钮的弹窗)
218
219> **说明:**
220>
221> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
222
223该示例实现了分别弹出一、二、三个按钮的弹窗。
224
225```ts
226// xxx.ets
227@Entry
228@Component
229struct AlertDialogExample {
230  build() {
231    Column({ space: 5 }) {
232      Button('one button dialog')
233        .onClick(() => {
234          // 建议使用this.getUIContext().showAlertDialog()
235          AlertDialog.show(
236            {
237              title: 'title',
238              message: 'text',
239              autoCancel: true,
240              alignment: DialogAlignment.Bottom,
241              offset: { dx: 0, dy: -20 },
242              gridCount: 3,
243              confirm: {
244                value: 'button',
245                action: () => {
246                  console.info('Button-clicking callback')
247                }
248              },
249              cancel: () => {
250                console.info('Closed callbacks')
251              },
252              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
253                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
254                console.log("dialog onWillDismiss")
255                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
256                  dismissDialogAction.dismiss()
257                }
258                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
259                  dismissDialogAction.dismiss()
260                }
261              }
262            }
263          )
264        })
265        .backgroundColor(0x317aff)
266      Button('two button dialog')
267        .onClick(() => {
268          // 建议使用this.getUIContext().showAlertDialog()
269          AlertDialog.show(
270            {
271              title: 'title',
272              subtitle: 'subtitle',
273              message: 'text',
274              autoCancel: true,
275              alignment: DialogAlignment.Bottom,
276              gridCount: 4,
277              offset: { dx: 0, dy: -20 },
278              primaryButton: {
279                value: 'cancel',
280                action: () => {
281                  console.info('Callback when the first button is clicked')
282                }
283              },
284              secondaryButton: {
285                enabled: true,
286                defaultFocus: true,
287                style: DialogButtonStyle.HIGHLIGHT,
288                value: 'ok',
289                action: () => {
290                  console.info('Callback when the second button is clicked')
291                }
292              },
293              cancel: () => {
294                console.info('Closed callbacks')
295              },
296              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
297                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
298                console.log("dialog onWillDismiss")
299                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
300                  dismissDialogAction.dismiss()
301                }
302                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
303                  dismissDialogAction.dismiss()
304                }
305              }
306            }
307          )
308        }).backgroundColor(0x317aff)
309        Button('three button dialog')
310        .onClick(() => {
311          // 建议使用this.getUIContext().showAlertDialog()
312          AlertDialog.show(
313            {
314              title: 'title',
315              subtitle: 'subtitle',
316              message: 'text',
317              autoCancel: true,
318              alignment: DialogAlignment.Bottom,
319              gridCount: 4,
320              offset: { dx: 0, dy: -20 },
321              buttonDirection: DialogButtonDirection.HORIZONTAL,
322              buttons: [
323                {
324                  value: '按钮',
325                  action: () => {
326                    console.info('Callback when button1 is clicked')
327                  }
328                },
329                {
330                  value: '按钮',
331                  action: () => {
332                    console.info('Callback when button2 is clicked')
333                  }
334                },
335                {
336                  value: '按钮',
337                  enabled: true,
338                  defaultFocus: true,
339                  style: DialogButtonStyle.HIGHLIGHT,
340                  action: () => {
341                    console.info('Callback when button3 is clicked')
342                  }
343                },
344              ],
345              cancel: () => {
346                console.info('Closed callbacks')
347              },
348              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
349                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
350                console.log("dialog onWillDismiss")
351                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
352                  dismissDialogAction.dismiss()
353                }
354                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
355                  dismissDialogAction.dismiss()
356                }
357              }
358            }
359          )
360        }).backgroundColor(0x317aff)
361    }.width('100%').margin({ top: 5 })
362  }
363}
364```
365
366![zh-cn_image_alert](figures/zh-cn_image_alert.gif)
367
368### 示例2(可在主窗外弹出的弹窗)
369
370该示例通过配置showInSubWindow为true弹出可以在主窗外显示的弹窗。
371
372> **说明:**
373>
374> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
375
376```ts
377// xxx.ets
378@Entry
379@Component
380struct AlertDialogExample {
381  build() {
382    Column({ space: 5 }) {
383      Button('one button dialog')
384        .onClick(() => {
385          // 建议使用this.getUIContext().showAlertDialog()
386          AlertDialog.show(
387            {
388              title: 'title',
389              subtitle: 'subtitle',
390              message: 'text',
391              autoCancel: true,
392              alignment: DialogAlignment.Center,
393              gridCount: 4,
394              showInSubWindow: true,
395              isModal: true,
396              offset: { dx: 0, dy: -20 },
397              buttonDirection: DialogButtonDirection.HORIZONTAL,
398              buttons: [
399                {
400                  value: '按钮',
401                  action: () => {
402                    console.info('Callback when button1 is clicked')
403                  }
404                },
405                {
406                  value: '按钮',
407                  action: () => {
408                    console.info('Callback when button2 is clicked')
409                  }
410                },
411                {
412                  value: '按钮',
413                  enabled: true,
414                  defaultFocus: true,
415                  style: DialogButtonStyle.HIGHLIGHT,
416                  action: () => {
417                    console.info('Callback when button3 is clicked')
418                  }
419                },
420              ],
421              cancel: () => {
422                console.info('Closed callbacks')
423              },
424              onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
425                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
426                console.log("dialog onWillDismiss")
427                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
428                  dismissDialogAction.dismiss()
429                }
430                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
431                  dismissDialogAction.dismiss()
432                }
433              }
434            })
435        })
436    }.width('100%').margin({ top: 5 }).backgroundColor(0x317aff)
437  }
438}
439```
440
441![zh-cn_image_alert_showinsubwindow](figures/zh-cn_image_alert_showinsubwindow.jpg)
442
443### 示例3(设置弹窗的动画)
444该示例通过配置transition实现弹窗的显示和消失动画。
445
446> **说明:**
447>
448> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
449
450```ts
451// xxx.ets
452@Entry
453@Component
454struct AlertDialogExample {
455  build() {
456    Column({ space: 5 }) {
457      Button('AlertDialog Set Duration')
458        .onClick(()=>{
459            // 建议使用this.getUIContext().showAlertDialog()
460            AlertDialog.show(
461              {
462                title: 'AlertDialog 1',
463                message: 'Set Animation Duration open 3 second, close 100ms',
464                autoCancel: true,
465                alignment: DialogAlignment.Top,
466                offset: { dx: 0, dy: -20 },
467                gridCount: 3,
468                transition:TransitionEffect.asymmetric(TransitionEffect.OPACITY
469                  .animation({ duration: 3000, curve: Curve.Sharp }).combine(TransitionEffect.scale({x: 1.5, y: 1.5}).animation({duration: 3000, curve: Curve.Sharp})),
470                  TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth })
471                    .combine(TransitionEffect.scale({x: 0.5, y: 0.5}).animation({duration: 100, curve: Curve.Smooth}))),
472                confirm: {
473                  value: 'button',
474                  action: () => {
475                    console.info('Button-clicking callback')
476                  }
477                },
478                cancel: () => {
479                  console.info('Closed callbacks')
480                }
481              }
482            )
483        })
484        .backgroundColor(0x317aff).height("88px")
485    }.width('100%').margin({ top: 5 })
486  }
487}
488```
489
490![zh-cn_image_alert_animation](figures/zh-cn_image_alert_animation.gif)
491
492### 示例4(设置弹窗的样式)
493该示例定义了AlertDialog的样式,如宽度、高度、背景色、阴影等等。
494
495> **说明:**
496>
497> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
498
499```ts
500// xxx.ets
501@Entry
502@Component
503struct AlertDialogExample {
504  build() {
505    Column({ space: 5 }) {
506      Button('one button dialog')
507        .onClick(() => {
508          // 建议使用this.getUIContext().showAlertDialog()
509          AlertDialog.show(
510            {
511              title: 'title',
512              message: 'text',
513              autoCancel: true,
514              alignment: DialogAlignment.Center,
515              offset: { dx: 0, dy: -20 },
516              gridCount: 3,
517              width: 300,
518              height: 200,
519              cornerRadius: 20,
520              borderWidth: 1,
521              borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
522              borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
523              backgroundColor: Color.White,
524              shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
525              textStyle: { wordBreak: WordBreak.BREAK_ALL },
526              confirm: {
527                value: 'button',
528                action: () => {
529                  console.info('Button-clicking callback')
530                }
531              },
532              cancel: () => {
533                console.info('Closed callbacks')
534              },
535              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
536                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
537                console.log("dialog onWillDismiss")
538                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
539                  dismissDialogAction.dismiss()
540                }
541                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
542                  dismissDialogAction.dismiss()
543                }
544              }
545            }
546          )
547        })
548        .backgroundColor(0x317aff)
549    }.width('100%').margin({ top: 5 })
550  }
551}
552```
553
554![zh-cn_image_alert_style](figures/zh-cn_image_alert_style.gif)
555
556### 示例5(悬停态弹窗)
557
558该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
559
560```ts
561@Entry
562@Component
563struct AlertDialogExample {
564  build() {
565    Column({ space: 5 }) {
566      Button('one button dialog')
567        .onClick(() => {
568          AlertDialog.show(
569            {
570              title: 'title',
571              message: 'text',
572              autoCancel: true,
573              alignment: DialogAlignment.Bottom,
574              gridCount: 3,
575              confirm: {
576                value: 'button',
577                action: () => {
578                  console.info('Button-clicking callback')
579                }
580              },
581              cancel: () => {
582                console.info('Closed callbacks')
583              },
584              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
585                console.info("reason=" + JSON.stringify(dismissDialogAction.reason));
586                console.log("dialog onWillDismiss");
587                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
588                  dismissDialogAction.dismiss();
589                }
590                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
591                  dismissDialogAction.dismiss();
592                }
593              },
594              enableHoverMode: true,
595              hoverModeArea: HoverModeAreaType.TOP_SCREEN
596            }
597          )
598        })
599        .backgroundColor(0x317aff)
600    }.width('100%').margin({ top: 5 })
601  }
602}
603```
604
605![zh-cn_image_alert](figures/zh-cn_image_alert_hovermode.gif)
606