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