1e41f4b71Sopenharmony_ci# Alert Dialog Box (AlertDialog) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can set the text content and response callback for an alert dialog box. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../js-apis-arkui-UIContext.md#uicontext). 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> Since API version 10, you can use the [showAlertDialog](../js-apis-arkui-UIContext.md#showalertdialog) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext) to obtain the UI context. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## AlertDialog.show 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_cistatic show(value: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions) 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciShows an alert dialog box. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**Parameters** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description| 26e41f4b71Sopenharmony_ci| ---- | --------------- | -------- | -------- | 27e41f4b71Sopenharmony_ci| value | [AlertDialogParamWithConfirm](#alertdialogparamwithconfirm) \| [AlertDialogParamWithButtons](#alertdialogparamwithbuttons) \| [AlertDialogParamWithOptions](#alertdialogparamwithoptions10)<sup>10+</sup> | Yes| Defines and displays the **\<AlertDialog>** component.| 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## AlertDialogParam 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 32e41f4b71Sopenharmony_ci| --------------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 33e41f4b71Sopenharmony_ci| title | [ResourceStr](ts-types.md#resourcestr) | No | Title of the dialog box.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 34e41f4b71Sopenharmony_ci| subtitle<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | No | Subtitle of the dialog box.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 35e41f4b71Sopenharmony_ci| message | [ResourceStr](ts-types.md#resourcestr) | Yes | Content of the dialog box.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 36e41f4b71Sopenharmony_ci| autoCancel | boolean | No | Whether to close the dialog box when the overlay is clicked. The value **true** means to close the dialog box when the overlay is clicked, and **false** means the opposite.<br>Default value: **true**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 37e41f4b71Sopenharmony_ci| cancel | () => void | No | Callback invoked when the dialog box is closed after the overlay is clicked.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 38e41f4b71Sopenharmony_ci| alignment | [DialogAlignment](#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**<br>**Atomic service API**: This API can be used in atomic services since API version 11.<br>**NOTE**<br>If **showInSubWindow** is set to **true** in **UIExtension**, the dialog box is aligned with the host window based on **UIExtension**.| 39e41f4b71Sopenharmony_ci| offset | [Offset](ts-types.md#offset) | No | Offset of the dialog box relative to the alignment position.<br>Default value: **{ dx: 0 , dy: 0 }**<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 40e41f4b71Sopenharmony_ci| gridCount | number | No | Number of grid columns occupied by the width of the dialog box.<br>Default value: **4**<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 41e41f4b71Sopenharmony_ci| maskRect<sup>10+</sup> | [Rectangle](#rectangle8) | No | Mask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.<br>Default value: **{ x: 0, y: 0, width: '100%', height: '100%' }**<br>**NOTE**<br>**maskRect** does not take effect when **showInSubWindow** is set to **true**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 42e41f4b71Sopenharmony_ci| showInSubWindow<sup>11+</sup> | boolean | No | Whether to show the dialog box in a sub-window when the dialog box needs to be displayed outside the main window.<br>Default value: **false**<br>**NOTE**<br>A dialog box whose **showInSubWindow** attribute is **true** cannot trigger the display of another dialog box whose **showInSubWindow** attribute is also **true**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 43e41f4b71Sopenharmony_ci| isModal<sup>11+</sup> | boolean | No | Whether the dialog box is a modal. A modal dialog box has a mask applied, while a non-modal dialog box does not.<br>Default value: **true**<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 44e41f4b71Sopenharmony_ci| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the dialog box.<br>Default value: **Color.Transparent**<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 45e41f4b71Sopenharmony_ci| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-appendix-enums.md#blurstyle9) | No | Background blur style of the dialog box.<br>Default value: **BlurStyle.COMPONENT_ULTRA_THICK**<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 46e41f4b71Sopenharmony_ci| onWillDismiss<sup>12+</sup> | Callback<[DismissDialogAction](#dismissdialogaction12)> | No | Callback for interactive closure of the dialog box.<br>**NOTE**<br>1. If this callback is registered, the dialog box will not be closed immediately after the user touches the mask or the Back button, presses the Esc key, or swipes left or right on the screen. The **reason** parameter in the callback is used to determine whether the dialog box can be closed. The reason returned by the component does not support the value **CLOSE_BUTTON**.<br>2. In the **onWillDismiss** callback, another **onWillDismiss** callback is not allowed.| 47e41f4b71Sopenharmony_ci| cornerRadius<sup>12+</sup> | [BorderRadiuses](ts-types.md#borderradiuses9) \| [Dimension](ts-types.md#dimension10) | No | Radius of the rounded corners of the background.<br>You can set separate radiuses for the four rounded corners.<br>Default value: **{ topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }**<br> The radius of the rounded corners is subject to the component size. Its maximum value is half of the component width or height. If the value is negative, the default value is used.<br> When set to a percentage, the value defines the radius as a percentage of the parent component's width or height.| 48e41f4b71Sopenharmony_ci| transition<sup>12+</sup> | [TransitionEffect](ts-transition-animation-component.md#transitioneffect10) | No | Transition effect for the entrance and exit of the dialog box.<br>**NOTE**<br> 1. If this parameter is not set, the default effect is used.<br> 2. Touching the Back button during the entrance animation pauses the entrance animation and starts the exit animation. The final effect is one obtained after the curves of the entrance and exit animations are combined.<br> 3. Touching the Back button during the exit animation does not affect the animation playback. Touching the Back button again closes the application. | 49e41f4b71Sopenharmony_ci| width<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | No | Width of the dialog box.<br>**NOTE**<br>- Default maximum width of the dialog box: 400 vp<br>- When this parameter is set to a percentage, the reference width of the dialog box is the width of the window where the dialog box is located. You can decrease or increase the width as needed.| 50e41f4b71Sopenharmony_ci| height<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | No | Height of the dialog box.<br>**NOTE**<br>- Default maximum height of the dialog box: 0.9 x (Window height – Safe area)<br>- When this parameter is set to a percentage, the reference height of the dialog box is the height of the window where the dialog box is located minus the safe area. You can decrease or increase the height as needed.| 51e41f4b71Sopenharmony_ci| borderWidth<sup>12+</sup> | [Dimension](ts-types.md#dimension10) \| [EdgeWidths](ts-types.md#edgewidths9) | No | <br>You can set the width for all four sides or set separate widths for individual sides.<br>Default value: **0**<br> When set to a percentage, the value defines the border width as a percentage of the parent dialog box's width.<br>If the left and right borders are greater than its width, or the top and bottom borders are greater than its height, the dialog box may not display as expected.| 52e41f4b71Sopenharmony_ci| borderColor<sup>12+</sup> | [ResourceColor](ts-types.md#resourcecolor) \| [EdgeColors](ts-types.md#edgecolors9) | No | Border color of the dialog box.<br>Default value: **Color.Black**<br> **borderColor** must be used with **borderWidth** in pairs.| 53e41f4b71Sopenharmony_ci| borderStyle<sup>12+</sup> | [BorderStyle](ts-appendix-enums.md#borderstyle) \| [EdgeStyles](ts-types.md#edgestyles9) | No | Border style of the dialog box.<br>Default value: **BorderStyle**<br> **borderStyle** must be used with **borderWidth** in pairs.| 54e41f4b71Sopenharmony_ci| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10) | No | Shadow of the dialog box.<br> Default value on 2-in-1 devices: **ShadowStyle.OUTER_FLOATING_MD** when the dialog box is focused and **ShadowStyle.OUTER_FLOATING_SM** otherwise| 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci## AlertDialogParamWithConfirm 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciInherits from [AlertDialogParam](#alertdialogparam). 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 63e41f4b71Sopenharmony_ci| ---------- | ---------------- | ---------- | ------------------------------- | 64e41f4b71Sopenharmony_ci| confirm | {<br>enabled<sup>10+</sup>?: boolean,<br>defaultFocus<sup>10+</sup>?: boolean,<br>style<sup>10+</sup>?: [DialogButtonStyle](#dialogbuttonstyle10),<br>value: [ResourceStr](ts-types.md#resourcestr),<br>fontColor?: [ResourceColor](ts-types.md#resourcecolor),<br>backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),<br>action: () => void<br>} | No | Information about the confirm button. When the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation, the button responds to the **Enter** key by default.<br>**enabled**: whether to respond when the button is clicked. The value **true** means to respond when the button is clicked, and **false** means the opposite.<br>Default value: **true**<br>**defaultFocus**: whether the button is the default focus. The value **true** means that the button is the default focus, and **false** means the opposite.<br>Default value: **false**<br>**style**: button style.<br>Default value: **DialogButtonStyle.DEFAULT**<br>**value**: button text.<br>**fontColor**: font color of the button.<br>**backgroundColor**: background color of the button.<br>**action**: callback upon button clicking.| 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciPriorities of the **confirm** parameters: **fontColor** and **backgroundColor** > **style** > **defaultFocus** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| backgroundColor | fontColor | style | defaultFocus | Effect | 69e41f4b71Sopenharmony_ci| --------------- | --------- | --------------------------- | ------------ | -------- | 70e41f4b71Sopenharmony_ci| Green | Red | - | - | Red text on green background| 71e41f4b71Sopenharmony_ci| Green | - | DialogButtonStyle.HIGHLIGHT | - | White text on green background| 72e41f4b71Sopenharmony_ci| Green | - | DialogButtonStyle.DEFAULT | - | Blue text on green background| 73e41f4b71Sopenharmony_ci| Green | - | - | TRUE | White text on green background| 74e41f4b71Sopenharmony_ci| Green | - | - | FALSE/- | Blue text on green background| 75e41f4b71Sopenharmony_ci| - | Red | DialogButtonStyle.HIGHLIGHT | - | Red text on blue background| 76e41f4b71Sopenharmony_ci| - | Red | DialogButtonStyle.DEFAULT | - | Red text on white background| 77e41f4b71Sopenharmony_ci| - | Red | - | TRUE | Red text on blue background| 78e41f4b71Sopenharmony_ci| - | Red | - | FALSE/- | Red text on white background| 79e41f4b71Sopenharmony_ci| - | - | DialogButtonStyle.HIGHLIGHT | - | White text on blue background| 80e41f4b71Sopenharmony_ci| - | - | DialogButtonStyle.DEFAULT | - | Blue text on white background| 81e41f4b71Sopenharmony_ci| - | - | - | TRUE | White text on blue background| 82e41f4b71Sopenharmony_ci| - | - | - | FALSE/- | Blue text on white background| 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci## AlertDialogParamWithButtons 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ciInherits from [AlertDialogParam](#alertdialogparam). 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 91e41f4b71Sopenharmony_ci| --------------- | ---------------------- | ------------ | --------------------- | 92e41f4b71Sopenharmony_ci| primaryButton | {<br>enabled<sup>10+</sup>?: boolean,<br>defaultFocus<sup>10+</sup>?: boolean,<br>style<sup>10+</sup>?: [DialogButtonStyle](#dialogbuttonstyle10),<br>value: [ResourceStr](ts-types.md#resourcestr),<br>fontColor?: [ResourceColor](ts-types.md#resourcecolor),<br>backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),<br>action: () => void;<br>} | No| Information about the confirm button. When the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation, the button responds to the **Enter** key by default.<br>**enabled**: whether to respond when the button is clicked.<br>Default value: **true**<br>**defaultFocus**: whether the button is the default focus.<br>Default value: **false**<br>**style**: button style.<br>Default value: **DialogButtonStyle.DEFAULT**<br>**value**: button text.<br>**fontColor**: font color of the button.<br>**backgroundColor**: background color of the button.<br>**action**: callback upon button clicking.| 93e41f4b71Sopenharmony_ci| secondaryButton | {<br>enabled<sup>10+</sup>?: boolean,<br>defaultFocus<sup>10+</sup>?: boolean,<br>style<sup>10+</sup>?: [DialogButtonStyle](#dialogbuttonstyle10),<br>value: [ResourceStr](ts-types.md#resourcestr),<br>fontColor?: [ResourceColor](ts-types.md#resourcecolor),<br>backgroundColor?: [ResourceColor](ts-types.md#resourcecolor),<br>action: () => void;<br>} | No | Information about the confirm button.<br>**enabled**: whether to respond when the button is clicked.<br>Default value: **true**<br>**defaultFocus**: whether the button is the default focus.<br>Default value: **false**<br>**style**: button style.<br>Default value: **DialogButtonStyle.DEFAULT**<br>**value**: button text.<br>**fontColor**: font color of the button.<br>**backgroundColor**: background color of the button.<br>**action**: callback upon button clicking.| 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci## AlertDialogParamWithOptions<sup>10+</sup> 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciInherits from [AlertDialogParam](#alertdialogparam). 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 102e41f4b71Sopenharmony_ci| --------------- | ---------------------- | ------------ | --------------------- | 103e41f4b71Sopenharmony_ci| buttons<sup>10+</sup> | Array<[AlertDialogButtonOptions](#alertdialogbuttonoptions10)> | Yes | Buttons in the dialog box.| 104e41f4b71Sopenharmony_ci|buttonDirection<sup>10+</sup> | [DialogButtonDirection](#dialogbuttondirection10)| No | Direction in which buttons are laid out.<br>Default value: **DialogButtonDirection.AUTO**<br>When there are more than three buttons, the Auto mode (which automatically switches to the vertical layout when there are more than two buttons) is recommended. In non-Auto mode, buttons that extend beyond the display area are clipped.| 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci## AlertDialogButtonOptions<sup>10+</sup> 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 111e41f4b71Sopenharmony_ci| ------------------| ---------------------- | ------------ | --------------------- | 112e41f4b71Sopenharmony_ci| enabled | boolean | No | Whether to respond when the button is clicked.<br>Default value: **true** | 113e41f4b71Sopenharmony_ci| defaultFocus | boolean | No | Whether the button is the default focus.<br>Default value: **false** | 114e41f4b71Sopenharmony_ci| style | [DialogButtonStyle](#dialogbuttonstyle10) | No | Style of the button.<br>Default value: **DialogButtonStyle.DEFAULT** | 115e41f4b71Sopenharmony_ci| value | [ResourceStr](ts-types.md#resourcestr) | Yes | Text of the button. If the value is null, the button is not displayed. | 116e41f4b71Sopenharmony_ci| fontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the button. | 117e41f4b71Sopenharmony_ci| backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the button. | 118e41f4b71Sopenharmony_ci| action | () => void | Yes | Callback upon button clicking. | 119e41f4b71Sopenharmony_ci| primary<sup>12+</sup> | boolean | No | Whether the button responds to the **Enter** key by default when the dialog box has focus and the **Tab** key is not pressed for sequential focus navigation. If there are multiple buttons, set this parameter to **true** for only one button. Otherwise, no button will respond.| 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci## DialogButtonDirection<sup>10+</sup> 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| Name | Description | 126e41f4b71Sopenharmony_ci| -------------------------- | --------- | 127e41f4b71Sopenharmony_ci| AUTO | Buttons are laid out horizontally when there are two or fewer buttons and vertically otherwise.| 128e41f4b71Sopenharmony_ci| HORIZONTAL | Buttons are laid out horizontally.| 129e41f4b71Sopenharmony_ci| VERTICAL | Buttons are laid out vertically.| 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci## DialogAlignment 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci| Name | Description | 136e41f4b71Sopenharmony_ci| ------------------------ | ------- | 137e41f4b71Sopenharmony_ci| Top | Vertical top alignment.| 138e41f4b71Sopenharmony_ci| Center | Vertical center alignment.| 139e41f4b71Sopenharmony_ci| Bottom | Vertical bottom alignment.| 140e41f4b71Sopenharmony_ci| Default | Default alignment. | 141e41f4b71Sopenharmony_ci| TopStart<sup>8+</sup> | Top left alignment. | 142e41f4b71Sopenharmony_ci| TopEnd<sup>8+</sup> | Top right alignment. | 143e41f4b71Sopenharmony_ci| CenterStart<sup>8+</sup> | Center left alignment. | 144e41f4b71Sopenharmony_ci| CenterEnd<sup>8+</sup> | Center right alignment. | 145e41f4b71Sopenharmony_ci| BottomStart<sup>8+</sup> | Bottom left alignment. | 146e41f4b71Sopenharmony_ci| BottomEnd<sup>8+</sup> | Bottom right alignment. | 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci## Rectangle<sup>8+</sup> 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciThe **Rectangle** type is used to represent a mask area of a dialog box. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 155e41f4b71Sopenharmony_ci|--------|------------------------------|----|-----------------------------------| 156e41f4b71Sopenharmony_ci| x | [Length](ts-types.md#length) | No | X coordinate of the mask area of the dialog box relative to the upper left corner of the window.<br>Default value: **0vp**| 157e41f4b71Sopenharmony_ci| y | [Length](ts-types.md#length) | No | Y coordinate of the mask area of the dialog box relative to the upper left corner of the window.<br>Default value: **0vp**| 158e41f4b71Sopenharmony_ci| width | [Length](ts-types.md#length) | No | Width of the mask area of the dialog box.<br>Default value: **'100%'** | 159e41f4b71Sopenharmony_ci| height | [Length](ts-types.md#length) | No | Height of the mask area of the dialog box.<br>Default value: **'100%'** | 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci> **NOTE** 162e41f4b71Sopenharmony_ci> 163e41f4b71Sopenharmony_ci> **x** and **y** can be set to a positive or negative percentage value. When **x** is set to **'100%'**, the mask area is moved rightward by the window width. When **x** is set to **'-100%'**, the mask area is moved leftward by the window width. When **y** is set to **'100%'**, the mask area is moved downward by the window height. When **y** is set to **'-100%'**, the mask area is moved upward by the window height. 164e41f4b71Sopenharmony_ci> 165e41f4b71Sopenharmony_ci> **width** and **height** can be set in percentage and can only be set to positive values. If they are set to negative values, the default values are used instead. 166e41f4b71Sopenharmony_ci> 167e41f4b71Sopenharmony_ci> The percentage is calculated based on the width and height of the window. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci## DialogButtonStyle<sup>10+</sup> 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci| Name | Description | 174e41f4b71Sopenharmony_ci| --------- | --------------------------------- | 175e41f4b71Sopenharmony_ci| DEFAULT | Blue text on white background (black background under the dark theme).| 176e41f4b71Sopenharmony_ci| HIGHLIGHT | White text on blue background. | 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## DismissDialogAction<sup>12+</sup> 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 181e41f4b71Sopenharmony_ci|----------|-------------------------------------|------|----------------------------------------------------------------| 182e41f4b71Sopenharmony_ci| dismiss | Callback<void> | Yes | Callback for closing the dialog box. This API is called only when the dialog box needs to be exited. | 183e41f4b71Sopenharmony_ci| reason | [DismissReason](#dismissreason12) | Yes | Reason why the dialog box cannot be closed. You must specify whether to close the dialog box for each of the listed actions.| 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci## DismissReason<sup>12+</sup> 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci| Name | Description | 188e41f4b71Sopenharmony_ci| --------------- | -------------------------------- | 189e41f4b71Sopenharmony_ci| PRESS_BACK | Touching the Back button, swiping left or right on the screen, or pressing the Esc key. | 190e41f4b71Sopenharmony_ci| TOUCH_OUTSIDE | Touching the mask. | 191e41f4b71Sopenharmony_ci| CLOSE_BUTTON | Touching the Close button. | 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci## Example 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci### Example 1 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci```ts 198e41f4b71Sopenharmony_ci// xxx.ets 199e41f4b71Sopenharmony_ci@Entry 200e41f4b71Sopenharmony_ci@Component 201e41f4b71Sopenharmony_cistruct AlertDialogExample { 202e41f4b71Sopenharmony_ci build() { 203e41f4b71Sopenharmony_ci Column({ space: 5 }) { 204e41f4b71Sopenharmony_ci Button('one button dialog') 205e41f4b71Sopenharmony_ci .onClick(() => { 206e41f4b71Sopenharmony_ci AlertDialog.show( 207e41f4b71Sopenharmony_ci { 208e41f4b71Sopenharmony_ci title: 'title', 209e41f4b71Sopenharmony_ci message: 'text', 210e41f4b71Sopenharmony_ci autoCancel: true, 211e41f4b71Sopenharmony_ci alignment: DialogAlignment.Bottom, 212e41f4b71Sopenharmony_ci offset: { dx: 0, dy: -20 }, 213e41f4b71Sopenharmony_ci gridCount: 3, 214e41f4b71Sopenharmony_ci confirm: { 215e41f4b71Sopenharmony_ci value: 'button', 216e41f4b71Sopenharmony_ci action: () => { 217e41f4b71Sopenharmony_ci console.info('Button-clicking callback') 218e41f4b71Sopenharmony_ci } 219e41f4b71Sopenharmony_ci }, 220e41f4b71Sopenharmony_ci cancel: () => { 221e41f4b71Sopenharmony_ci console.info('Closed callbacks') 222e41f4b71Sopenharmony_ci }, 223e41f4b71Sopenharmony_ci onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { 224e41f4b71Sopenharmony_ci console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 225e41f4b71Sopenharmony_ci console.log("dialog onWillDismiss") 226e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { 227e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 228e41f4b71Sopenharmony_ci } 229e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { 230e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci } 233e41f4b71Sopenharmony_ci } 234e41f4b71Sopenharmony_ci ) 235e41f4b71Sopenharmony_ci }) 236e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 237e41f4b71Sopenharmony_ci Button('two button dialog') 238e41f4b71Sopenharmony_ci .onClick(() => { 239e41f4b71Sopenharmony_ci AlertDialog.show( 240e41f4b71Sopenharmony_ci { 241e41f4b71Sopenharmony_ci title: 'title', 242e41f4b71Sopenharmony_ci subtitle: 'subtitle', 243e41f4b71Sopenharmony_ci message: 'text', 244e41f4b71Sopenharmony_ci autoCancel: true, 245e41f4b71Sopenharmony_ci alignment: DialogAlignment.Bottom, 246e41f4b71Sopenharmony_ci gridCount: 4, 247e41f4b71Sopenharmony_ci offset: { dx: 0, dy: -20 }, 248e41f4b71Sopenharmony_ci primaryButton: { 249e41f4b71Sopenharmony_ci value: 'cancel', 250e41f4b71Sopenharmony_ci action: () => { 251e41f4b71Sopenharmony_ci console.info('Callback when the first button is clicked') 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci }, 254e41f4b71Sopenharmony_ci secondaryButton: { 255e41f4b71Sopenharmony_ci enabled: true, 256e41f4b71Sopenharmony_ci defaultFocus: true, 257e41f4b71Sopenharmony_ci style: DialogButtonStyle.HIGHLIGHT, 258e41f4b71Sopenharmony_ci value: 'ok', 259e41f4b71Sopenharmony_ci action: () => { 260e41f4b71Sopenharmony_ci console.info('Callback when the second button is clicked') 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci }, 263e41f4b71Sopenharmony_ci cancel: () => { 264e41f4b71Sopenharmony_ci console.info('Closed callbacks') 265e41f4b71Sopenharmony_ci }, 266e41f4b71Sopenharmony_ci onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { 267e41f4b71Sopenharmony_ci console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 268e41f4b71Sopenharmony_ci console.log("dialog onWillDismiss") 269e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { 270e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 271e41f4b71Sopenharmony_ci } 272e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { 273e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 274e41f4b71Sopenharmony_ci } 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci ) 278e41f4b71Sopenharmony_ci }).backgroundColor(0x317aff) 279e41f4b71Sopenharmony_ci Button('three button dialog') 280e41f4b71Sopenharmony_ci .onClick(() => { 281e41f4b71Sopenharmony_ci AlertDialog.show( 282e41f4b71Sopenharmony_ci { 283e41f4b71Sopenharmony_ci title: 'title', 284e41f4b71Sopenharmony_ci subtitle: 'subtitle', 285e41f4b71Sopenharmony_ci message: 'text', 286e41f4b71Sopenharmony_ci autoCancel: true, 287e41f4b71Sopenharmony_ci alignment: DialogAlignment.Bottom, 288e41f4b71Sopenharmony_ci gridCount: 4, 289e41f4b71Sopenharmony_ci offset: { dx: 0, dy: -20 }, 290e41f4b71Sopenharmony_ci buttonDirection: DialogButtonDirection.HORIZONTAL, 291e41f4b71Sopenharmony_ci buttons: [ 292e41f4b71Sopenharmony_ci { 293e41f4b71Sopenharmony_ci value: 'Button', 294e41f4b71Sopenharmony_ci action: () => { 295e41f4b71Sopenharmony_ci console.info('Callback when button1 is clicked') 296e41f4b71Sopenharmony_ci } 297e41f4b71Sopenharmony_ci }, 298e41f4b71Sopenharmony_ci { 299e41f4b71Sopenharmony_ci value: 'Button', 300e41f4b71Sopenharmony_ci action: () => { 301e41f4b71Sopenharmony_ci console.info('Callback when button2 is clicked') 302e41f4b71Sopenharmony_ci } 303e41f4b71Sopenharmony_ci }, 304e41f4b71Sopenharmony_ci { 305e41f4b71Sopenharmony_ci value: 'Button', 306e41f4b71Sopenharmony_ci enabled: true, 307e41f4b71Sopenharmony_ci defaultFocus: true, 308e41f4b71Sopenharmony_ci style: DialogButtonStyle.HIGHLIGHT, 309e41f4b71Sopenharmony_ci action: () => { 310e41f4b71Sopenharmony_ci console.info('Callback when button3 is clicked') 311e41f4b71Sopenharmony_ci } 312e41f4b71Sopenharmony_ci }, 313e41f4b71Sopenharmony_ci ], 314e41f4b71Sopenharmony_ci cancel: () => { 315e41f4b71Sopenharmony_ci console.info('Closed callbacks') 316e41f4b71Sopenharmony_ci }, 317e41f4b71Sopenharmony_ci onWillDismiss:(dismissDialogAction: DismissDialogAction)=> { 318e41f4b71Sopenharmony_ci console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 319e41f4b71Sopenharmony_ci console.log("dialog onWillDismiss") 320e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { 321e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 322e41f4b71Sopenharmony_ci } 323e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { 324e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 325e41f4b71Sopenharmony_ci } 326e41f4b71Sopenharmony_ci } 327e41f4b71Sopenharmony_ci } 328e41f4b71Sopenharmony_ci ) 329e41f4b71Sopenharmony_ci }).backgroundColor(0x317aff) 330e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 331e41f4b71Sopenharmony_ci } 332e41f4b71Sopenharmony_ci} 333e41f4b71Sopenharmony_ci``` 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci### Example 2 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci```ts 340e41f4b71Sopenharmony_ci// xxx.ets 341e41f4b71Sopenharmony_ci@Entry 342e41f4b71Sopenharmony_ci@Component 343e41f4b71Sopenharmony_cistruct AlertDialogExample { 344e41f4b71Sopenharmony_ci build() { 345e41f4b71Sopenharmony_ci Column({ space: 5 }) { 346e41f4b71Sopenharmony_ci Button('one button dialog') 347e41f4b71Sopenharmony_ci .onClick(() => { 348e41f4b71Sopenharmony_ci AlertDialog.show( 349e41f4b71Sopenharmony_ci { 350e41f4b71Sopenharmony_ci title: 'title', 351e41f4b71Sopenharmony_ci subtitle: 'subtitle', 352e41f4b71Sopenharmony_ci message: 'text', 353e41f4b71Sopenharmony_ci autoCancel: true, 354e41f4b71Sopenharmony_ci alignment: DialogAlignment.Center, 355e41f4b71Sopenharmony_ci gridCount: 4, 356e41f4b71Sopenharmony_ci showInSubWindow: true, 357e41f4b71Sopenharmony_ci isModal: true, 358e41f4b71Sopenharmony_ci offset: { dx: 0, dy: -20 }, 359e41f4b71Sopenharmony_ci buttonDirection: DialogButtonDirection.HORIZONTAL, 360e41f4b71Sopenharmony_ci buttons: [ 361e41f4b71Sopenharmony_ci { 362e41f4b71Sopenharmony_ci value: 'Button', 363e41f4b71Sopenharmony_ci action: () => { 364e41f4b71Sopenharmony_ci console.info('Callback when button1 is clicked') 365e41f4b71Sopenharmony_ci } 366e41f4b71Sopenharmony_ci }, 367e41f4b71Sopenharmony_ci { 368e41f4b71Sopenharmony_ci value: 'Button', 369e41f4b71Sopenharmony_ci action: () => { 370e41f4b71Sopenharmony_ci console.info('Callback when button2 is clicked') 371e41f4b71Sopenharmony_ci } 372e41f4b71Sopenharmony_ci }, 373e41f4b71Sopenharmony_ci { 374e41f4b71Sopenharmony_ci value: 'Button', 375e41f4b71Sopenharmony_ci enabled: true, 376e41f4b71Sopenharmony_ci defaultFocus: true, 377e41f4b71Sopenharmony_ci style: DialogButtonStyle.HIGHLIGHT, 378e41f4b71Sopenharmony_ci action: () => { 379e41f4b71Sopenharmony_ci console.info('Callback when button3 is clicked') 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci }, 382e41f4b71Sopenharmony_ci ], 383e41f4b71Sopenharmony_ci cancel: () => { 384e41f4b71Sopenharmony_ci console.info('Closed callbacks') 385e41f4b71Sopenharmony_ci }, 386e41f4b71Sopenharmony_ci onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 387e41f4b71Sopenharmony_ci console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 388e41f4b71Sopenharmony_ci console.log("dialog onWillDismiss") 389e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { 390e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 391e41f4b71Sopenharmony_ci } 392e41f4b71Sopenharmony_ci if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { 393e41f4b71Sopenharmony_ci dismissDialogAction.dismiss() 394e41f4b71Sopenharmony_ci } 395e41f4b71Sopenharmony_ci } 396e41f4b71Sopenharmony_ci }) 397e41f4b71Sopenharmony_ci }) 398e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }).backgroundColor(0x317aff) 399e41f4b71Sopenharmony_ci } 400e41f4b71Sopenharmony_ci} 401e41f4b71Sopenharmony_ci``` 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci### Example 3 406e41f4b71Sopenharmony_ciThis example sets a 3s entrance animation and a 100 ms exit animation for the alert dialog. 407e41f4b71Sopenharmony_ci```ts 408e41f4b71Sopenharmony_ci// xxx.ets 409e41f4b71Sopenharmony_ciimport router from '@ohos.router'; 410e41f4b71Sopenharmony_ci@Entry 411e41f4b71Sopenharmony_ci@Component 412e41f4b71Sopenharmony_cistruct AlertDialogExample { 413e41f4b71Sopenharmony_ci build() { 414e41f4b71Sopenharmony_ci Column({ space: 5 }) { 415e41f4b71Sopenharmony_ci Button('AlertDialog Set Duration') 416e41f4b71Sopenharmony_ci .onClick(()=>{ 417e41f4b71Sopenharmony_ci AlertDialog.show( 418e41f4b71Sopenharmony_ci { 419e41f4b71Sopenharmony_ci title: 'AlertDialog 1', 420e41f4b71Sopenharmony_ci message: 'Set Animation Duration open 3 second, close 100ms', 421e41f4b71Sopenharmony_ci autoCancel: true, 422e41f4b71Sopenharmony_ci alignment: DialogAlignment.Top, 423e41f4b71Sopenharmony_ci offset: { dx: 0, dy: -20 }, 424e41f4b71Sopenharmony_ci gridCount: 3, 425e41f4b71Sopenharmony_ci transition:TransitionEffect.asymmetric(TransitionEffect.OPACITY 426e41f4b71Sopenharmony_ci .animation({ duration: 3000, curve: Curve.Sharp }).combine(TransitionEffect.scale({x: 1.5, y: 1.5}).animation({duration: 3000, curve: Curve.Sharp})), 427e41f4b71Sopenharmony_ci TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) 428e41f4b71Sopenharmony_ci .combine(TransitionEffect.scale({x: 0.5, y: 0.5}).animation({duration: 100, curve: Curve.Smooth}))), 429e41f4b71Sopenharmony_ci confirm: { 430e41f4b71Sopenharmony_ci value: 'button', 431e41f4b71Sopenharmony_ci action: () => { 432e41f4b71Sopenharmony_ci console.info('Button-clicking callback') 433e41f4b71Sopenharmony_ci } 434e41f4b71Sopenharmony_ci }, 435e41f4b71Sopenharmony_ci cancel: () => { 436e41f4b71Sopenharmony_ci console.info('Closed callbacks') 437e41f4b71Sopenharmony_ci } 438e41f4b71Sopenharmony_ci } 439e41f4b71Sopenharmony_ci ) 440e41f4b71Sopenharmony_ci }) 441e41f4b71Sopenharmony_ci .backgroundColor(0x317aff).height("88px") 442e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 443e41f4b71Sopenharmony_ci } 444e41f4b71Sopenharmony_ci} 445e41f4b71Sopenharmony_ci``` 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci 448