1# @ohos.promptAction (Prompt) 2 3The **PromptAction** module provides APIs for creating and showing toasts, dialog boxes, and action menus. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> This module cannot be used in the file declaration of the [UIAbility](../apis-ability-kit/js-apis-app-ability-uiAbility.md). In other words, the APIs of this module can be used only after a component instance is created; they cannot be called in the lifecycle of the UIAbility. 10> 11> 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). 12> 13> Since API version 10, you can use the [getPromptAction](js-apis-arkui-UIContext.md#getpromptaction) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to obtain the [PromptAction](js-apis-arkui-UIContext.md#promptaction) object associated with the current UI context. 14 15## Modules to Import 16 17```ts 18import promptAction from '@ohos.promptAction'; 19``` 20 21## promptAction.showToast 22 23showToast(options: ShowToastOptions): void 24 25Shows a toast. 26 27**System capability**: SystemCapability.ArkUI.ArkUI.Full 28 29**Parameters** 30 31| Name | Type | Mandatory | Description | 32| ------- | ------------------------------------- | ---- | ------- | 33| options | [ShowToastOptions](#showtoastoptions) | Yes | Toast options.| 34 35**Error codes** 36 37For details about the error codes, see [promptAction Error Codes](errorcode-promptAction.md). 38 39| ID | Error Message| 40| --------- | ------- | 41| 100001 | if UI execution context not found. | 42 43**Example** 44 45```ts 46import promptAction from '@ohos.promptAction' 47import { BusinessError } from '@ohos.base'; 48try { 49 promptAction.showToast({ 50 message: 'Message Info', 51 duration: 2000 52 }); 53} catch (error) { 54 let message = (error as BusinessError).message 55 let code = (error as BusinessError).code 56 console.error(`showToast args error code is ${code}, message is ${message}`); 57}; 58 59``` 60 61 62 63## ShowToastOptions 64 65Describes the options for showing the toast. 66 67**System capability**: SystemCapability.ArkUI.ArkUI.Full 68 69| Name | Type | Mandatory| Description | 70| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 71| message | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>9+</sup>| Yes | Text to display.<br>**NOTE**<br>The default font is **'Harmony Sans'**. Other fonts are not supported.| 72| duration | number | No | Duration that the toast will remain on the screen. The default value is 1500 ms. The value range is 1500 ms to 10000 ms. If a value less than 1500 ms is set, the default value is used. If the value greater than 10000 ms is set, the upper limit 10000 ms is used.| 73| bottom | string\| number | No | Distance between the toast border and the bottom of the screen.<br>Default value: **80vp** | 74| showMode<sup>11+</sup> | [ToastShowMode](#toastshowmode11) | No | Whether to show the toast above the application.<br>Default value: **ToastShowMode.DEFAULT**, which means to show the toast within the application | 75 76### ToastShowMode<sup>11+</sup> 77 78Describes the mode in which the toast is shown. 79 80**System capability**: SystemCapability.ArkUI.ArkUI.Full 81 82| Name | Value| Description | 83| -------- | ---- | ------------------------------------------------------------ | 84| DEFAULT | 0 | The toast is shown within the application.| 85| TOP_MOST | 1 | The toast is shown above the application.| 86 87 88## promptAction.showDialog 89 90showDialog(options: ShowDialogOptions): Promise<ShowDialogSuccessResponse> 91 92Shows a dialog box. This API uses a promise to return the result asynchronously. 93 94**System capability**: SystemCapability.ArkUI.ArkUI.Full 95 96**Parameters** 97 98| Name | Type | Mandatory | Description | 99| ------- | --------------------------------------- | ---- | ------ | 100| options | [ShowDialogOptions](#showdialogoptions) | Yes | Dialog box options.| 101 102**Return value** 103 104| Type | Description | 105| ---------------------------------------- | -------- | 106| Promise<[ShowDialogSuccessResponse](#showdialogsuccessresponse)> | Promise used to return the dialog box response result.| 107 108**Error codes** 109 110For details about the error codes, see [promptAction Error Codes](errorcode-promptAction.md). 111 112| ID | Error Message| 113| --------- | ------- | 114| 100001 | if UI execution context not found. | 115 116**Example** 117 118```ts 119import promptAction from '@ohos.promptAction' 120import { BusinessError } from '@ohos.base'; 121try { 122 promptAction.showDialog({ 123 title: 'Title Info', 124 message: 'Message Info', 125 buttons: [ 126 { 127 text: 'button1', 128 color: '#000000' 129 }, 130 { 131 text: 'button2', 132 color: '#000000' 133 } 134 ], 135 }) 136 .then(data => { 137 console.info('showDialog success, click button: ' + data.index); 138 }) 139 .catch((err:Error) => { 140 console.info('showDialog error: ' + err); 141 }) 142} catch (error) { 143 let message = (error as BusinessError).message 144 let code = (error as BusinessError).code 145 console.error(`showDialog args error code is ${code}, message is ${message}`); 146}; 147``` 148 149 150 151## promptAction.showDialog 152 153showDialog(options: ShowDialogOptions, callback: AsyncCallback<ShowDialogSuccessResponse>):void 154 155Shows a dialog box. This API uses an asynchronous callback to return the result. 156 157**System capability**: SystemCapability.ArkUI.ArkUI.Full 158 159**Parameters** 160 161| Name | Type | Mandatory | Description | 162| -------- | ---------------------------------------- | ---- | ------------ | 163| options | [ShowDialogOptions](#showdialogoptions) | Yes | Dialog box options.| 164| callback | AsyncCallback<[ShowDialogSuccessResponse](#showdialogsuccessresponse)> | Yes | Callback used to return the dialog box response result. | 165 166**Error codes** 167 168For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 169 170| ID | Error Message| 171| --------- | ------- | 172| 100001 | if UI execution context not found. | 173 174**Example** 175 176```ts 177import promptAction from '@ohos.promptAction'; 178import { BusinessError } from '@ohos.base'; 179try { 180 promptAction.showDialog({ 181 title: 'showDialog Title Info', 182 message: 'Message Info', 183 buttons: [ 184 { 185 text: 'button1', 186 color: '#000000' 187 }, 188 { 189 text: 'button2', 190 color: '#000000' 191 } 192 ] 193 }, (err, data) => { 194 if (err) { 195 console.info('showDialog err: ' + err); 196 return; 197 } 198 console.info('showDialog success callback, click button: ' + data.index); 199 }); 200} catch (error) { 201 let message = (error as BusinessError).message 202 let code = (error as BusinessError).code 203 console.error(`showDialog args error code is ${code}, message is ${message}`); 204}; 205``` 206 207 208 209When the **showInSubWindow** attribute is set to **true**, the toast can be displayed outside the window. 210 211```ts 212import promptAction from '@ohos.promptAction'; 213import { BusinessError } from '@ohos.base'; 214try { 215 promptAction.showDialog({ 216 title: 'showDialog Title Info', 217 message: 'Message Info', 218 isModal: true, 219 showInSubWindow: true, 220 buttons: [ 221 { 222 text: 'button1', 223 color: '#000000' 224 }, 225 { 226 text: 'button2', 227 color: '#000000' 228 } 229 ] 230 }, (err, data) => { 231 if (err) { 232 console.info('showDialog err: ' + err); 233 return; 234 } 235 console.info('showDialog success callback, click button: ' + data.index); 236 }); 237} catch (error) { 238 let message = (error as BusinessError).message 239 let code = (error as BusinessError).code 240 console.error(`showDialog args error code is ${code}, message is ${message}`); 241}; 242``` 243 244 245 246 247 248## ShowDialogOptions 249 250Describes the options for showing the dialog box. 251 252**System capability**: SystemCapability.ArkUI.ArkUI.Full 253 254| Name | Type | Mandatory| Description | 255| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 256| title | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| No | Title of the dialog box. | 257| message | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| No | Text body. | 258| buttons | Array<[Button](#button)> | No | Array of buttons in the dialog box. The array structure is {text:'button', color: '\#666666'}. More than one button is supported. 259| alignment<sup>10+</sup> | [DialogAlignment](../arkui-ts/ts-methods-alert-dialog-box.md#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**| 260| offset<sup>10+</sup> | [Offset](../arkui-ts/ts-types.md#offset) | No | Offset of the dialog box based on the **alignment** settings.<br>Default value: **{ dx: 0 , dy: 0 }**| 261| maskRect<sup>10+</sup>| [Rectangle](../arkui-ts/ts-methods-alert-dialog-box.md#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%' }**| 262| showInSubWindow<sup>11+</sup> | boolean | No| Whether to show the dialog box in a sub-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**.| 263| 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**| 264 265## ShowDialogSuccessResponse 266 267Describes the dialog box response result. 268 269**System capability**: SystemCapability.ArkUI.ArkUI.Full 270 271| Name | Type | Mandatory| Description | 272| ----- | ------ | ---- | ------------------------------- | 273| index | number | No | Index of the selected button in the **buttons** array.| 274 275## promptAction.showActionMenu 276 277showActionMenu(options: ActionMenuOptions, callback: AsyncCallback<ActionMenuSuccessResponse>):void 278 279Shows an action menu. This API uses a callback to return the result asynchronously. 280 281**System capability**: SystemCapability.ArkUI.ArkUI.Full 282 283**Parameters** 284 285| Name | Type | Mandatory | Description | 286| -------- | ---------------------------------------- | ---- | --------- | 287| options | [ActionMenuOptions](#actionmenuoptions) | Yes | Action menu options. | 288| callback | AsyncCallback<[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | Yes | Callback used to return the action menu response result.| 289 290**Error codes** 291 292For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 293 294| ID | Error Message| 295| --------- | ------- | 296| 100001 | if UI execution context not found. | 297 298**Example** 299 300```ts 301import promptAction from '@ohos.promptAction'; 302import { BusinessError } from '@ohos.base'; 303try { 304 promptAction.showActionMenu({ 305 title: 'Title Info', 306 buttons: [ 307 { 308 text: 'item1', 309 color: '#666666' 310 }, 311 { 312 text: 'item2', 313 color: '#000000' 314 }, 315 ] 316 }, (err, data) => { 317 if (err) { 318 console.info('showActionMenu err: ' + err); 319 return; 320 } 321 console.info('showActionMenu success callback, click button: ' + data.index); 322 }) 323} catch (error) { 324 let message = (error as BusinessError).message 325 let code = (error as BusinessError).code 326 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 327}; 328``` 329 330 331 332## promptAction.showActionMenu 333 334showActionMenu(options: ActionMenuOptions): Promise<ActionMenuSuccessResponse> 335 336Shows an action menu. This API uses a promise to return the result asynchronously. 337 338**System capability**: SystemCapability.ArkUI.ArkUI.Full 339 340**Parameters** 341 342| Name | Type | Mandatory | Description | 343| ------- | --------------------------------------- | ---- | ------- | 344| options | [ActionMenuOptions](#actionmenuoptions) | Yes | Action menu options.| 345 346**Return value** 347 348| Type | Description | 349| ---------------------------------------- | ------- | 350| Promise<[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | Promise used to return the action menu response result.| 351 352**Error codes** 353 354For details about the error codes, see [promptAction Error Codes](errorcode-promptAction.md). 355 356| ID | Error Message| 357| --------- | ------- | 358| 100001 | if UI execution context not found. | 359 360**Example** 361 362```ts 363import promptAction from '@ohos.promptAction'; 364import { BusinessError } from '@ohos.base'; 365try { 366 promptAction.showActionMenu({ 367 title: 'showActionMenu Title Info', 368 buttons: [ 369 { 370 text: 'item1', 371 color: '#666666' 372 }, 373 { 374 text: 'item2', 375 color: '#000000' 376 }, 377 ] 378 }) 379 .then(data => { 380 console.info('showActionMenu success, click button: ' + data.index); 381 }) 382 .catch((err:Error) => { 383 console.info('showActionMenu error: ' + err); 384 }) 385} catch (error) { 386 let message = (error as BusinessError).message 387 let code = (error as BusinessError).code 388 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 389}; 390``` 391 392 393 394## promptAction.openCustomDialog<sup>11+</sup> 395 396openCustomDialog(options: CustomDialogOptions): Promise<number> 397 398Opens a custom dialog box. 399 400This API cannot be used in [ServiceExtension](../../../application-dev/application-models/serviceextensionability.md). 401 402**isModal = true** and **showInSubWindow = true** cannot be used at the same time. 403 404By default, the width of the dialog box is four columns in portrait mode and five columns in landscape mode. 405 406**System capability**: SystemCapability.ArkUI.ArkUI.Full 407 408**Parameters** 409 410| Name | Type | Mandatory| Description | 411| ------- | --------------------------------------------- | ---- | ------------------ | 412| options | [CustomDialogOptions](#customdialogoptions11) | Yes | Content of the custom dialog box.| 413 414**Return value** 415 416| Type | Description | 417| --------------------- | ------------------------------------- | 418| Promise<number> | ID of the custom dialog box, which can be used in **closeCustomDialog**.| 419 420**Error codes** 421 422For details about the error codes, see [promptAction Error Codes](errorcode-promptAction.md). 423 424| ID| Error Message | 425| -------- | ---------------------------------- | 426| 100001 | if UI execution context not found. | 427 428**Example** 429 430```ts 431import promptAction from '@ohos.promptAction' 432let customDialogId: number = 0 433@Builder 434function customDialogBuilder() { 435 Column() { 436 Text('Custom dialog Message').fontSize(10) 437 Row() { 438 Button("OK").onClick(() => { 439 promptAction.closeCustomDialog(customDialogId) 440 }) 441 Blank().width(50) 442 Button("Cancel").onClick(() => { 443 promptAction.closeCustomDialog(customDialogId) 444 }) 445 } 446 }.height(200).padding(5) 447} 448 449@Entry 450@Component 451struct Index { 452 @State message: string = 'Hello World' 453 454 build() { 455 Row() { 456 Column() { 457 Text(this.message) 458 .fontSize(50) 459 .fontWeight(FontWeight.Bold) 460 .onClick(() => { 461 promptAction.openCustomDialog({ 462 builder: customDialogBuilder.bind(this) 463 }).then((dialogId: number) => { 464 customDialogId = dialogId 465 }) 466 }) 467 } 468 .width('100%') 469 } 470 .height('100%') 471 } 472} 473``` 474 475## promptAction.closeCustomDialog<sup>11+</sup> 476 477closeCustomDialog(dialogId: number): void 478 479Closes the specified custom dialog box. 480 481**System capability**: SystemCapability.ArkUI.ArkUI.Full 482 483**Parameters** 484 485| Name | Type | Mandatory| Description | 486| -------- | ------ | ---- | -------------------------------- | 487| dialogId | number | Yes | ID of the custom dialog box to close. It is returned from **openCustomDialog**.| 488 489**Error codes** 490 491For details about the error codes, see [promptAction Error Codes](errorcode-promptAction.md). 492 493| ID| Error Message | 494| -------- | ---------------------------------- | 495| 100001 | if UI execution context not found. | 496 497**Example** 498 499See the example of [promptAction.openCustomDialog](#promptactionopencustomdialog11). 500 501## ActionMenuOptions 502 503Describes the options for showing the action menu. 504 505**System capability**: SystemCapability.ArkUI.ArkUI.Full 506 507| Name | Type | Mandatory| Description | 508| ----------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 509| title | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>9+</sup>| No | Title of the dialog box. | 510| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?] | Yes | Array of menu item buttons. The array structure is **{text:'button', color: '\#666666'}**. Up to six buttons are supported. If there are more than six buttons, only the first six buttons will be displayed.| 511| 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**, indicating that the dialog box is not displayed in the subwindow<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**.| 512| 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**| 513 514## CustomDialogOptions<sup>11+</sup> 515 516Defines the options of the custom dialog box. This API extends [BaseDialogOptions](#basedialogoptions11). 517 518**System capability**: SystemCapability.ArkUI.ArkUI.Full 519 520| Name | Type | Mandatory| Description | 521| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | 522| builder | [CustomBuilder](../arkui-ts/ts-types.md#custombuilder8) | No | Content of the custom dialog box.<br>**NOTE**<br>**bind(this)** must be used for the builder.<br>The aspect ratio of the root node is relative to the size of the dialog box container.<br>The aspect ratio of a non-root node is relative to the size of the parent node.| 523 524## BaseDialogOptions<sup>11+</sup> 525 526Defines the options of the dialog box. 527 528**System capability**: SystemCapability.ArkUI.ArkUI.Full 529 530| Name | Type | Mandatory| Description | 531| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 532| maskRect | [Rectangle](../arkui-ts/ts-methods-alert-dialog-box.md#rectangle8) | No | Mask area. | 533| alignment | [DialogAlignment](../arkui-ts/ts-methods-alert-dialog-box.md#dialogalignment) | No | Alignment mode of the dialog box in the vertical direction. | 534| offset | [Offset](../arkui-ts/ts-types.md#offset) | No | Offset of the dialog box based on the **alignment** settings. | 535| isModal | 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**| 536| showInSubWindow | boolean | No | Whether to show the dialog box in a sub-window.<br>Default value: **false**| 537 538## ActionMenuSuccessResponse 539 540Describes the action menu response result. 541 542**System capability**: SystemCapability.ArkUI.ArkUI.Full 543 544| Name | Type | Mandatory | Description | 545| ----- | ------ | ---- | ------------------------ | 546| index | number | No | Index of the selected button in the **buttons** array, starting from **0**.| 547 548## Button 549 550Describes the menu item button in the action menu. 551 552**System capability**: SystemCapability.ArkUI.ArkUI.Full 553 554| Name | Type | Mandatory | Description | 555| ----- | ---------------------------------------- | ---- | ------- | 556| text | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| Yes | Button text.| 557| color | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| Yes | Text color of the button.| 558<!--no_check-->