• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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](./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';
19import { BusinessError } from '@ohos.base';
20```
21
22## promptAction.showToast
23
24showToast(options: ShowToastOptions): void
25
26Shows a toast.
27
28**System capability**: SystemCapability.ArkUI.ArkUI.Full
29
30**Parameters**
31
32| Name    | Type                                   | Mandatory  | Description     |
33| ------- | ------------------------------------- | ---- | ------- |
34| options | [ShowToastOptions](#showtoastoptions) | Yes   | Toast options.|
35
36**Error codes**
37
38For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md).
39
40| ID  | Error Message|
41| --------- | ------- |
42| 100001    | if UI execution context not found. |
43
44**Example**
45
46```ts
47import promptAction from '@ohos.promptAction'
48import { BusinessError } from '@ohos.base';
49try {
50  promptAction.showToast({
51    message: 'Message Info',
52    duration: 2000
53  });
54} catch (error) {
55  let message = (error as BusinessError).message
56  let code = (error as BusinessError).code
57  console.error(`showToast args error code is ${code}, message is ${message}`);
58};
59
60```
61
62![en-us_image_0001](figures/en-us_image_0001.gif)
63
64## ShowToastOptions
65
66Describes the options for showing the toast.
67
68**System capability**: SystemCapability.ArkUI.ArkUI.Full
69
70| Name    | Type                                                        | Mandatory| Description                                                        |
71| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
72| 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.|
73| 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.|
74| bottom   | string\| number                                              | No  | Distance between the toast border and the bottom of the screen.<br>Default value: **80vp**            |
75
76## promptAction.showDialog
77
78showDialog(options: ShowDialogOptions): Promise&lt;ShowDialogSuccessResponse&gt;
79
80Shows a dialog box. This API uses a promise to return the result synchronously.
81
82**System capability**: SystemCapability.ArkUI.ArkUI.Full
83
84**Parameters**
85
86| Name    | Type                                     | Mandatory  | Description    |
87| ------- | --------------------------------------- | ---- | ------ |
88| options | [ShowDialogOptions](#showdialogoptions) | Yes   | Dialog box options.|
89
90**Return value**
91
92| Type                                      | Description      |
93| ---------------------------------------- | -------- |
94| Promise&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | Promise used to return the dialog box response result.|
95
96**Error codes**
97
98For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md).
99
100| ID  | Error Message|
101| --------- | ------- |
102| 100001    | if UI execution context not found. |
103
104**Example**
105
106```ts
107import promptAction from '@ohos.promptAction'
108import { BusinessError } from '@ohos.base';
109try {
110  promptAction.showDialog({
111    title: 'Title Info',
112    message: 'Message Info',
113    buttons: [
114      {
115        text: 'button1',
116        color: '#000000'
117      },
118      {
119        text: 'button2',
120        color: '#000000'
121      }
122    ],
123  })
124    .then(data => {
125      console.info('showDialog success, click button: ' + data.index);
126    })
127    .catch((err:Error) => {
128      console.info('showDialog error: ' + err);
129    })
130} catch (error) {
131  let message = (error as BusinessError).message
132  let code = (error as BusinessError).code
133  console.error(`showDialog args error code is ${code}, message is ${message}`);
134};
135```
136
137![en-us_image_0002](figures/en-us_image_0002.gif)
138
139## promptAction.showDialog
140
141showDialog(options: ShowDialogOptions, callback: AsyncCallback&lt;ShowDialogSuccessResponse&gt;):void
142
143Shows a dialog box. This API uses an asynchronous callback to return the result.
144
145**System capability**: SystemCapability.ArkUI.ArkUI.Full
146
147**Parameters**
148
149| Name     | Type                                      | Mandatory  | Description          |
150| -------- | ---------------------------------------- | ---- | ------------ |
151| options  | [ShowDialogOptions](#showdialogoptions)  | Yes   | Dialog box options.|
152| callback | AsyncCallback&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | Yes   | Callback used to return the dialog box response result.  |
153
154**Error codes**
155
156For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md).
157
158| ID  | Error Message|
159| --------- | ------- |
160| 100001    | if UI execution context not found. |
161
162**Example**
163
164```ts
165import promptAction from '@ohos.promptAction';
166import { BusinessError } from '@ohos.base';
167try {
168  promptAction.showDialog({
169    title: 'showDialog Title Info',
170    message: 'Message Info',
171    buttons: [
172      {
173        text: 'button1',
174        color: '#000000'
175      },
176      {
177        text: 'button2',
178        color: '#000000'
179      }
180    ]
181  }, (err, data) => {
182    if (err) {
183      console.info('showDialog err: ' + err);
184      return;
185    }
186    console.info('showDialog success callback, click button: ' + data.index);
187  });
188} catch (error) {
189  let message = (error as BusinessError).message
190  let code = (error as BusinessError).code
191  console.error(`showDialog args error code is ${code}, message is ${message}`);
192};
193```
194
195![en-us_image_0002](figures/en-us_image_0002.gif)
196
197## ShowDialogOptions
198
199Describes the options for showing the dialog box.
200
201**System capability**: SystemCapability.ArkUI.ArkUI.Full
202
203| Name   | Type                                                        | Mandatory| Description                                                        |
204| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
205| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| No  | Title of the dialog box.                                                  |
206| message | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| No  | Text body.                                                  |
207| buttons  | Array&lt;[Button](#button)&gt;    | No  | Array of buttons in the dialog box. The array structure is {text:'button', color: '\#666666'}. More than one button is supported.
208| 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**|
209| 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 }**|
210| maskRect<sup>10+</sup>| [Rectangle](../arkui-ts/ts-methods-alert-dialog-box.md#rectangle10) | 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%' }**|
211
212## ShowDialogSuccessResponse
213
214Describes the dialog box response result.
215
216**System capability**: SystemCapability.ArkUI.ArkUI.Full
217
218| Name | Type  | Mandatory| Description                           |
219| ----- | ------ | ---- | ------------------------------- |
220| index | number | No  | Index of the selected button in the **buttons** array.|
221
222## promptAction.showActionMenu
223
224showActionMenu(options: ActionMenuOptions, callback: AsyncCallback&lt;ActionMenuSuccessResponse&gt;):void
225
226Shows an action menu. This API uses a callback to return the result asynchronously.
227
228**System capability**: SystemCapability.ArkUI.ArkUI.Full
229
230**Parameters**
231
232| Name     | Type                                      | Mandatory  | Description       |
233| -------- | ---------------------------------------- | ---- | --------- |
234| options  | [ActionMenuOptions](#actionmenuoptions)  | Yes   | Action menu options.  |
235| callback | AsyncCallback&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | Yes   | Callback used to return the action menu response result.|
236
237**Error codes**
238
239For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md).
240
241| ID  | Error Message|
242| --------- | ------- |
243| 100001    | if UI execution context not found. |
244
245**Example**
246
247```ts
248import promptAction from '@ohos.promptAction';
249import { BusinessError } from '@ohos.base';
250try {
251  promptAction.showActionMenu({
252    title: 'Title Info',
253    buttons: [
254      {
255        text: 'item1',
256        color: '#666666'
257      },
258      {
259        text: 'item2',
260        color: '#000000'
261      },
262    ]
263  }, (err, data) => {
264    if (err) {
265      console.info('showActionMenu err: ' + err);
266      return;
267    }
268    console.info('showActionMenu success callback, click button: ' + data.index);
269  })
270} catch (error) {
271  let message = (error as BusinessError).message
272  let code = (error as BusinessError).code
273  console.error(`showActionMenu args error code is ${code}, message is ${message}`);
274};
275```
276
277![en-us_image_0005](figures/en-us_image_0005.gif)
278
279## promptAction.showActionMenu
280
281showActionMenu(options: ActionMenuOptions): Promise&lt;ActionMenuSuccessResponse&gt;
282
283Shows an action menu. This API uses a promise to return the result synchronously.
284
285**System capability**: SystemCapability.ArkUI.ArkUI.Full
286
287**Parameters**
288
289| Name    | Type                                     | Mandatory  | Description     |
290| ------- | --------------------------------------- | ---- | ------- |
291| options | [ActionMenuOptions](#actionmenuoptions) | Yes   | Action menu options.|
292
293**Return value**
294
295| Type                                      | Description     |
296| ---------------------------------------- | ------- |
297| Promise&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)&gt; | Promise used to return the action menu response result.|
298
299**Error codes**
300
301For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md).
302
303| ID  | Error Message|
304| --------- | ------- |
305| 100001    | if UI execution context not found. |
306
307**Example**
308
309```ts
310import promptAction from '@ohos.promptAction';
311import { BusinessError } from '@ohos.base';
312try {
313  promptAction.showActionMenu({
314    title: 'showActionMenu Title Info',
315    buttons: [
316      {
317        text: 'item1',
318        color: '#666666'
319      },
320      {
321        text: 'item2',
322        color: '#000000'
323      },
324    ]
325  })
326    .then(data => {
327      console.info('showActionMenu success, click button: ' + data.index);
328    })
329    .catch((err:Error) => {
330      console.info('showActionMenu error: ' + err);
331    })
332} catch (error) {
333  let message = (error as BusinessError).message
334  let code = (error as BusinessError).code
335  console.error(`showActionMenu args error code is ${code}, message is ${message}`);
336};
337```
338
339![en-us_image_0005](figures/en-us_image_0005.gif)
340
341## ActionMenuOptions
342
343Describes the options for showing the action menu.
344
345**System capability**: SystemCapability.ArkUI.ArkUI.Full
346
347| Name   | Type                                                        | Mandatory| Description                                                        |
348| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
349| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| No  | Title of the dialog box.                                                  |
350| 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.|
351
352## ActionMenuSuccessResponse
353
354Describes the action menu response result.
355
356**System capability**: SystemCapability.ArkUI.ArkUI.Full
357
358| Name   | Type    | Mandatory  | Description                      |
359| ----- | ------ | ---- | ------------------------ |
360| index | number | No   | Index of the selected button in the **buttons** array, starting from **0**.|
361
362## Button
363
364Describes the menu item button in the action menu.
365
366**System capability**: SystemCapability.ArkUI.ArkUI.Full
367
368| Name   | Type                                      | Mandatory  | Description     |
369| ----- | ---------------------------------------- | ---- | ------- |
370| text  | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| Yes   | Button text.|
371| color | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup>| Yes   | Text color of the button.|
372