• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
61## AlertDialogParamWithConfirm对象说明
62
63继承自[AlertDialogParam](#alertdialogparam对象说明)。
64
65**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
66
67**系统能力:** SystemCapability.ArkUI.ArkUI.Full
68
69| 名称       | 类型     | 必填     | 说明         |
70| ---------- | ---------------- | ---------- | ------------------------------- |
71| 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选中时的回调。 |
72
73confirm参数优先级:fontColor、backgroundColor  > style > defaultFocus
74
75| backgroundColor | fontColor | style                       | defaultFocus | 效果     |
76| --------------- | --------- | --------------------------- | ------------ | -------- |
77| 绿底            | 红字      | -                           | -            | 绿底红字 |
78| 绿底            | -         | DialogButtonStyle.HIGHLIGHT | -            | 绿底白字 |
79| 绿底            | -         | DialogButtonStyle.DEFAULT   | -            | 绿底蓝字 |
80| 绿底            | -         | -                           | TRUE         | 绿底白字 |
81| 绿底            | -         | -                           | FALSE/-      | 绿底蓝字 |
82| -               | 红字      | DialogButtonStyle.HIGHLIGHT | -            | 蓝底红字 |
83| -               | 红字      | DialogButtonStyle.DEFAULT   | -            | 白底红字 |
84| -               | 红字      | -                           | TRUE         | 蓝底红字 |
85| -               | 红字      | -                           | FALSE/-      | 白底红字 |
86| -               | -         | DialogButtonStyle.HIGHLIGHT | -            | 蓝底白字 |
87| -               | -         | DialogButtonStyle.DEFAULT   | -            | 白底蓝字 |
88| -               | -         | -                           | TRUE         | 蓝底白字 |
89| -               | -         | -                           | FALSE/-      | 白底蓝字 |
90
91## AlertDialogParamWithButtons对象说明
92
93继承自[AlertDialogParam](#alertdialogparam对象说明)。
94
95**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
96
97**系统能力:** SystemCapability.ArkUI.ArkUI.Full
98
99| 名称             | 类型                | 必填     | 说明                     |
100| --------------- | ---------------------- | ------------ | --------------------- |
101| 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选中时的回调。 |
102| 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选中时的回调。 |
103
104## AlertDialogParamWithOptions<sup>10+</sup>对象说明
105
106继承自[AlertDialogParam](#alertdialogparam对象说明)。
107
108**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
109
110**系统能力:** SystemCapability.ArkUI.ArkUI.Full
111
112| 名称             | 类型                | 必填     | 说明                    |
113| --------------- | ---------------------- | ------------ | --------------------- |
114| buttons<sup>10+</sup>       | Array&lt;[AlertDialogButtonOptions](#alertdialogbuttonoptions10对象说明)&gt;                 | 是  | 弹窗容器中的多个按钮。 |
115|buttonDirection<sup>10+</sup>      | [DialogButtonDirection](#dialogbuttondirection10枚举说明)| 否  | 按钮排布方向默认值为DialogButtonDirection.AUTO,建议3个以上按钮使用Auto模式(两个以上按钮会切换为纵向模式,通常能显示更多按钮),非Auto模式下,3个以上按钮可能会显示不全,超出显示范围的按钮会被截断。|
116
117## AlertDialogButtonOptions<sup>10+</sup>对象说明
118
119**系统能力:** SystemCapability.ArkUI.ArkUI.Full
120
121| 名称             | 类型                | 必填     | 说明                    |
122| ------------------| ---------------------- | ------------ | --------------------- |
123| enabled           | boolean | 否     | 点击Button是否响应,默认值true。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
124| defaultFocus           | boolean | 否     | 设置Button是否是默认焦点,默认值false。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
125| style           | [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10) | 否     | 设置Button的风格样式,默认值DialogButtonStyle.DEFAULT。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
126| value           | [ResourceStr](ts-types.md#resourcestr) | 是     | Button的文本内容,若值为null,则该按钮不显示。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
127| fontColor           | [ResourceColor](ts-types.md#resourcecolor) | 否     | Button的文本颜色。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
128| backgroundColor           | [ResourceColor](ts-types.md#resourcecolor) | 否     | Button背景颜色。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
129| action           | 	() => void | 是     | Button选中时的回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
130| primary<sup>12+</sup> |	boolean | 否    | 在弹窗获焦且未进行tab键走焦时,按钮是否默认响应Enter键。多个Button时,只允许一个Button的该字段配置为true,否则所有Button均不响应。多重弹窗可自动获焦连续响应。在defaultFocus为true时不生效。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
131
132## DialogButtonDirection<sup>10+</sup>枚举说明
133
134**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
135
136**系统能力:** SystemCapability.ArkUI.ArkUI.Full
137
138| 名称                       | 说明    |
139| -------------------------- | --------- |
140| AUTO                      | 两个及以下按钮水平排布,两个以上为竖直排布。 |
141| HORIZONTAL                      | 按钮水平布局。 |
142| VERTICAL                      | 按钮竖直布局。 |
143
144## DialogAlignment枚举说明
145
146**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
147
148**系统能力:** SystemCapability.ArkUI.ArkUI.Full
149
150| 名称                     | 说明           |
151| ------------------------ | -------------- |
152| Top                      | 垂直顶部对齐。 |
153| Center                   | 垂直居中对齐。 |
154| Bottom                   | 垂直底部对齐。 |
155| Default                  | 默认对齐。     |
156| TopStart<sup>8+</sup>    | 左上对齐。     |
157| TopEnd<sup>8+</sup>      | 右上对齐。     |
158| CenterStart<sup>8+</sup> | 左中对齐。     |
159| CenterEnd<sup>8+</sup>   | 右中对齐。     |
160| BottomStart<sup>8+</sup> | 左下对齐。     |
161| BottomEnd<sup>8+</sup>   | 右下对齐。     |
162
163## Rectangle<sup>8+</sup>类型说明
164
165Rectangle是各种Dialog中maskRect参数的类型。
166
167**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
168
169**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
170
171**系统能力:** SystemCapability.ArkUI.ArkUI.Full
172
173| 名称     | 类型                           | 必填 | 说明                               |
174|--------|------------------------------|----|-----------------------------------|
175| x      | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域相对于窗口左上角的x轴坐标。<br/>默认值:0vp |
176| y      | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域相对于窗口左上角的y轴坐标。<br/>默认值:0vp |
177| width  | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域的宽度。<br/>默认值:'100%'        |
178| height | [Length](ts-types.md#length) | 否  | 弹窗遮蔽层区域的高度。<br/>默认值:'100%'        |
179
180>  **说明:**
181>
182>  x和y可以设置正负值百分比。当x设置为'100%'时表示遮蔽层区域往右偏移窗口本身宽度大小,当x设置为'-100%'时表示遮蔽层区域往左偏移窗口本身宽度大小。当y设置为'100%'时表示遮蔽层区域往下偏移窗口本身高度大小,当y设置为'-100%'时表示遮蔽层区域往上偏移窗口本身高度大小。
183>
184>  width和height只能设置正值,支持百分比,如果设置为负值,那么该值将被重置为默认值。
185>
186>  百分比相对于窗口自身宽高进行计算。
187
188## DismissDialogAction<sup>12+</sup>
189
190Dialog关闭的信息。
191
192**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
193
194**系统能力:** SystemCapability.ArkUI.ArkUI.Full
195
196### 属性
197
198| 名称    | 类型                                                         | 可读 | 可写 | 说明                                                         |
199| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
200| dismiss | Callback&lt;void&gt;                                         | 否   | 否   | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 |
201| reason  | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否   | 否   | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 |
202
203## TextStyle<sup>12+</sup>对象说明
204
205**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
206
207**系统能力:** SystemCapability.ArkUI.ArkUI.Full
208
209| 名称     | 类型                           | 必填 | 说明                                |
210|--------|------------------------------|----|-----------------------------------|
211| wordBreak      | [WordBreak](ts-appendix-enums.md#wordbreak11) | 否  | 弹窗message内容的文本截断方式。<br/>默认值:WordBreak.BREAK_ALL |
212
213## 示例
214
215### 示例1
216
217> **说明:**
218>
219> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
220
221分别弹出一、二、三个按钮的弹窗。
222
223```ts
224// xxx.ets
225@Entry
226@Component
227struct AlertDialogExample {
228  build() {
229    Column({ space: 5 }) {
230      Button('one button dialog')
231        .onClick(() => {
232          // 建议使用this.getUIContext().showAlertDialog()
233          AlertDialog.show(
234            {
235              title: 'title',
236              message: 'text',
237              autoCancel: true,
238              alignment: DialogAlignment.Bottom,
239              offset: { dx: 0, dy: -20 },
240              gridCount: 3,
241              confirm: {
242                value: 'button',
243                action: () => {
244                  console.info('Button-clicking callback')
245                }
246              },
247              cancel: () => {
248                console.info('Closed callbacks')
249              },
250              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
251                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
252                console.log("dialog onWillDismiss")
253                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
254                  dismissDialogAction.dismiss()
255                }
256                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
257                  dismissDialogAction.dismiss()
258                }
259              }
260            }
261          )
262        })
263        .backgroundColor(0x317aff)
264      Button('two button dialog')
265        .onClick(() => {
266          // 建议使用this.getUIContext().showAlertDialog()
267          AlertDialog.show(
268            {
269              title: 'title',
270              subtitle: 'subtitle',
271              message: 'text',
272              autoCancel: true,
273              alignment: DialogAlignment.Bottom,
274              gridCount: 4,
275              offset: { dx: 0, dy: -20 },
276              primaryButton: {
277                value: 'cancel',
278                action: () => {
279                  console.info('Callback when the first button is clicked')
280                }
281              },
282              secondaryButton: {
283                enabled: true,
284                defaultFocus: true,
285                style: DialogButtonStyle.HIGHLIGHT,
286                value: 'ok',
287                action: () => {
288                  console.info('Callback when the second button is clicked')
289                }
290              },
291              cancel: () => {
292                console.info('Closed callbacks')
293              },
294              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
295                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
296                console.log("dialog onWillDismiss")
297                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
298                  dismissDialogAction.dismiss()
299                }
300                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
301                  dismissDialogAction.dismiss()
302                }
303              }
304            }
305          )
306        }).backgroundColor(0x317aff)
307        Button('three button dialog')
308        .onClick(() => {
309          // 建议使用this.getUIContext().showAlertDialog()
310          AlertDialog.show(
311            {
312              title: 'title',
313              subtitle: 'subtitle',
314              message: 'text',
315              autoCancel: true,
316              alignment: DialogAlignment.Bottom,
317              gridCount: 4,
318              offset: { dx: 0, dy: -20 },
319              buttonDirection: DialogButtonDirection.HORIZONTAL,
320              buttons: [
321                {
322                  value: '按钮',
323                  action: () => {
324                    console.info('Callback when button1 is clicked')
325                  }
326                },
327                {
328                  value: '按钮',
329                  action: () => {
330                    console.info('Callback when button2 is clicked')
331                  }
332                },
333                {
334                  value: '按钮',
335                  enabled: true,
336                  defaultFocus: true,
337                  style: DialogButtonStyle.HIGHLIGHT,
338                  action: () => {
339                    console.info('Callback when button3 is clicked')
340                  }
341                },
342              ],
343              cancel: () => {
344                console.info('Closed callbacks')
345              },
346              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
347                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
348                console.log("dialog onWillDismiss")
349                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
350                  dismissDialogAction.dismiss()
351                }
352                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
353                  dismissDialogAction.dismiss()
354                }
355              }
356            }
357          )
358        }).backgroundColor(0x317aff)
359    }.width('100%').margin({ top: 5 })
360  }
361}
362```
363
364![zh-cn_image_alert](figures/zh-cn_image_alert.gif)
365
366### 示例2
367
368弹出可展示在主窗外的弹窗。
369
370> **说明:**
371>
372> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
373
374```ts
375// xxx.ets
376@Entry
377@Component
378struct AlertDialogExample {
379  build() {
380    Column({ space: 5 }) {
381      Button('one button dialog')
382        .onClick(() => {
383          // 建议使用this.getUIContext().showAlertDialog()
384          AlertDialog.show(
385            {
386              title: 'title',
387              subtitle: 'subtitle',
388              message: 'text',
389              autoCancel: true,
390              alignment: DialogAlignment.Center,
391              gridCount: 4,
392              showInSubWindow: true,
393              isModal: true,
394              offset: { dx: 0, dy: -20 },
395              buttonDirection: DialogButtonDirection.HORIZONTAL,
396              buttons: [
397                {
398                  value: '按钮',
399                  action: () => {
400                    console.info('Callback when button1 is clicked')
401                  }
402                },
403                {
404                  value: '按钮',
405                  action: () => {
406                    console.info('Callback when button2 is clicked')
407                  }
408                },
409                {
410                  value: '按钮',
411                  enabled: true,
412                  defaultFocus: true,
413                  style: DialogButtonStyle.HIGHLIGHT,
414                  action: () => {
415                    console.info('Callback when button3 is clicked')
416                  }
417                },
418              ],
419              cancel: () => {
420                console.info('Closed callbacks')
421              },
422              onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
423                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
424                console.log("dialog onWillDismiss")
425                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
426                  dismissDialogAction.dismiss()
427                }
428                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
429                  dismissDialogAction.dismiss()
430                }
431              }
432            })
433        })
434    }.width('100%').margin({ top: 5 }).backgroundColor(0x317aff)
435  }
436}
437```
438
439![zh-cn_image_alert_showinsubwindow](figures/zh-cn_image_alert_showinsubwindow.jpg)
440
441### 示例3
442AlertDialog显示动画持续3秒,退出动画持续100毫秒
443
444> **说明:**
445>
446> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
447
448```ts
449// xxx.ets
450@Entry
451@Component
452struct AlertDialogExample {
453  build() {
454    Column({ space: 5 }) {
455      Button('AlertDialog Set Duration')
456        .onClick(()=>{
457            // 建议使用this.getUIContext().showAlertDialog()
458            AlertDialog.show(
459              {
460                title: 'AlertDialog 1',
461                message: 'Set Animation Duration open 3 second, close 100ms',
462                autoCancel: true,
463                alignment: DialogAlignment.Top,
464                offset: { dx: 0, dy: -20 },
465                gridCount: 3,
466                transition:TransitionEffect.asymmetric(TransitionEffect.OPACITY
467                  .animation({ duration: 3000, curve: Curve.Sharp }).combine(TransitionEffect.scale({x: 1.5, y: 1.5}).animation({duration: 3000, curve: Curve.Sharp})),
468                  TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth })
469                    .combine(TransitionEffect.scale({x: 0.5, y: 0.5}).animation({duration: 100, curve: Curve.Smooth}))),
470                confirm: {
471                  value: 'button',
472                  action: () => {
473                    console.info('Button-clicking callback')
474                  }
475                },
476                cancel: () => {
477                  console.info('Closed callbacks')
478                }
479              }
480            )
481        })
482        .backgroundColor(0x317aff).height("88px")
483    }.width('100%').margin({ top: 5 })
484  }
485}
486```
487
488![zh-cn_image_alert_animation](figures/zh-cn_image_alert_animation.gif)
489
490### 示例4
491该示例定义了AlertDialog的样式,如宽度、高度、背景色、阴影等等
492
493> **说明:**
494>
495> 直接使用AlertDialog可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog)调用绑定实例的AlertDialog.show()。
496
497```ts
498// xxx.ets
499@Entry
500@Component
501struct AlertDialogExample {
502  build() {
503    Column({ space: 5 }) {
504      Button('one button dialog')
505        .onClick(() => {
506          // 建议使用this.getUIContext().showAlertDialog()
507          AlertDialog.show(
508            {
509              title: 'title',
510              message: 'text',
511              autoCancel: true,
512              alignment: DialogAlignment.Center,
513              offset: { dx: 0, dy: -20 },
514              gridCount: 3,
515              width: 300,
516              height: 200,
517              cornerRadius: 20,
518              borderWidth: 1,
519              borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
520              borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
521              backgroundColor: Color.White,
522              shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
523              textStyle: { wordBreak: WordBreak.BREAK_ALL },
524              confirm: {
525                value: 'button',
526                action: () => {
527                  console.info('Button-clicking callback')
528                }
529              },
530              cancel: () => {
531                console.info('Closed callbacks')
532              },
533              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
534                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
535                console.log("dialog onWillDismiss")
536                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
537                  dismissDialogAction.dismiss()
538                }
539                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
540                  dismissDialogAction.dismiss()
541                }
542              }
543            }
544          )
545        })
546        .backgroundColor(0x317aff)
547    }.width('100%').margin({ top: 5 })
548  }
549}
550```
551
552![zh-cn_image_alert_style](figures/zh-cn_image_alert_style.gif)
553