js-apis-prompt.md 9.8 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.prompt (Prompt)
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4
The **Prompt** module provides APIs for creating and showing toasts, dialog boxes, and action menus.

W
wusongqing 已提交
5
> **NOTE**
W
wusongqing 已提交
6
>
7 8
> The APIs of this module are deprecated since API Version 9. You are advised to use [@ohos.promptAction](js-apis-promptAction.md) instead.
>
W
wusongqing 已提交
9 10 11 12
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

W
wusongqing 已提交
13
```js
W
wusongqing 已提交
14 15 16 17 18 19 20
import prompt from '@ohos.prompt'
```

## prompt.showToast

showToast(options: ShowToastOptions): void

W
wusongqing 已提交
21
Shows a toast.
W
wusongqing 已提交
22 23 24 25

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
G
Gloria 已提交
26

W
wusongqing 已提交
27 28 29
| Name    | Type                                   | Mandatory  | Description     |
| ------- | ------------------------------------- | ---- | ------- |
| options | [ShowToastOptions](#showtoastoptions) | Yes   | Toast options.|
W
wusongqing 已提交
30 31

**Example**
G
Gloria 已提交
32 33

```js
W
wusongqing 已提交
34 35 36 37
prompt.showToast({            
  message: 'Message Info',
    duration: 2000,      
});
G
Gloria 已提交
38 39
```

40 41
![en-us_image_0001](figures/en-us_image_0001.gif)

W
wusongqing 已提交
42 43 44 45 46 47
## ShowToastOptions

Describes the options for showing the toast.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

G
Gloria 已提交
48 49
| Name      | Type                                      | Mandatory  | Description                                      |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
50
| message  | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | Yes   | Text to display.                                |
G
Gloria 已提交
51
| 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.|
52
| bottom   | string\| number                          | No   | Distance between the toast border and the bottom of the screen. It does not have an upper limit. The default unit is vp.               |
W
wusongqing 已提交
53 54 55 56 57 58 59 60 61 62

## prompt.showDialog

showDialog(options: ShowDialogOptions): Promise&lt;ShowDialogSuccessResponse&gt;

Shows a dialog box. This API uses a promise to return the result synchronously.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
G
Gloria 已提交
63

W
wusongqing 已提交
64 65 66
| Name    | Type                                     | Mandatory  | Description    |
| ------- | --------------------------------------- | ---- | ------ |
| options | [ShowDialogOptions](#showdialogoptions) | Yes   | Dialog box options.|
W
wusongqing 已提交
67 68 69

**Return value**

W
wusongqing 已提交
70 71 72
| Type                                      | Description      |
| ---------------------------------------- | -------- |
| Promise&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | Promise used to return the dialog box response result.|
W
wusongqing 已提交
73 74 75

**Example**

G
Gloria 已提交
76
```js
W
wusongqing 已提交
77 78 79 80 81 82 83 84 85 86 87
prompt.showDialog({
  title: 'Title Info',
  message: 'Message Info',
  buttons: [
    {
      text: 'button1',
      color: '#000000',
    },
    {
      text: 'button2',
      color: '#000000',
W
wusongqing 已提交
88
    }
W
wusongqing 已提交
89 90 91 92 93 94 95 96
  ],
})
  .then(data => {
    console.info('showDialog success, click button: ' + data.index);
  })
  .catch(err => {
    console.info('showDialog error: ' + err);
  })
G
Gloria 已提交
97
```
W
wusongqing 已提交
98

99 100
![en-us_image_0002](figures/en-us_image_0002.gif)

W
wusongqing 已提交
101 102 103 104
## prompt.showDialog

showDialog(options: ShowDialogOptions, callback: AsyncCallback&lt;ShowDialogSuccessResponse&gt;):void 

W
wusongqing 已提交
105
Shows a dialog box. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
106 107 108 109

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
G
Gloria 已提交
110

W
wusongqing 已提交
111 112 113 114
| Name     | Type                                      | Mandatory  | Description          |
| -------- | ---------------------------------------- | ---- | ------------ |
| options  | [ShowDialogOptions](#showdialogoptions)  | Yes   | Dialog box options.|
| callback | AsyncCallback&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | Yes   | Callback used to return the dialog box response result.  |
W
wusongqing 已提交
115 116

**Example**
G
Gloria 已提交
117 118

```js
W
wusongqing 已提交
119 120 121 122 123 124 125
prompt.showDialog({
  title: 'showDialog Title Info',
  message: 'Message Info',
  buttons: [
    {
      text: 'button1',
      color: '#000000',
W
wusongqing 已提交
126
    },
W
wusongqing 已提交
127 128 129
    {
      text: 'button2',
      color: '#000000',
W
wusongqing 已提交
130
    }
W
wusongqing 已提交
131 132 133 134 135
  ]
}, (err, data) => {
  if (err) {
    console.info('showDialog err: ' + err);
    return;
W
wusongqing 已提交
136
  }
W
wusongqing 已提交
137 138
  console.info('showDialog success callback, click button: ' + data.index);
});
G
Gloria 已提交
139
```
W
wusongqing 已提交
140

141 142
![en-us_image_0004](figures/en-us_image_0004.gif)

W
wusongqing 已提交
143 144 145 146 147 148
## ShowDialogOptions

Describes the options for showing the dialog box.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
149 150 151 152 153
| Name   | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | No  | Title of the dialog box.                                                  |
| message | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | No  | Text body.                                                  |
| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?]    | No  | Array of buttons in the dialog box. The array structure is **{text:'button', color: '\#666666'}**. Up to three buttons are supported. The first button is of the **positiveButton** type, the second is of the **negativeButton** type, and the third is of the **neutralButton** type.|
W
wusongqing 已提交
154 155 156 157 158 159 160

## ShowDialogSuccessResponse 

Describes the dialog box response result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
161 162 163
| Name | Type  | Mandatory| Description                           |
| ----- | ------ | ---- | ------------------------------- |
| index | number | No  | Index of the selected button in the **buttons** array.|
W
wusongqing 已提交
164 165 166 167 168 169 170 171 172 173 174


## prompt.showActionMenu

showActionMenu(options: ActionMenuOptions, callback: AsyncCallback&lt;ActionMenuSuccessResponse&gt;):void

Shows an action menu. This API uses a callback to return the result asynchronously.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
G
Gloria 已提交
175

W
wusongqing 已提交
176 177 178 179
| Name     | Type                                      | Mandatory  | Description       |
| -------- | ---------------------------------------- | ---- | --------- |
| options  | [ActionMenuOptions](#actionmenuoptions)  | Yes   | Action menu options.  |
| callback | AsyncCallback&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | Yes   | Callback used to return the action menu response result.|
W
wusongqing 已提交
180 181

**Example**
G
Gloria 已提交
182 183

```js
W
wusongqing 已提交
184 185 186 187 188 189
prompt.showActionMenu({
  title: 'Title Info',
  buttons: [
    {
      text: 'item1',
      color: '#666666',
W
wusongqing 已提交
190
    },
W
wusongqing 已提交
191 192 193 194 195 196 197 198 199
    {
      text: 'item2',
      color: '#000000',
    },
  ]
}, (err, data) => {
  if (err) {
    console.info('showActionMenu err: ' + err);
    return;
W
wusongqing 已提交
200
  }
W
wusongqing 已提交
201 202
  console.info('showActionMenu success callback, click button: ' + data.index);
})
G
Gloria 已提交
203
```
W
wusongqing 已提交
204

205 206
![en-us_image_0005](figures/en-us_image_0005.gif) 

W
wusongqing 已提交
207 208
## prompt.showActionMenu

W
wusongqing 已提交
209
showActionMenu(options: ActionMenuOptions): Promise&lt;ActionMenuSuccessResponse&gt;
W
wusongqing 已提交
210 211 212 213 214 215

Shows an action menu. This API uses a promise to return the result synchronously.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**
G
Gloria 已提交
216

W
wusongqing 已提交
217 218 219
| Name    | Type                                     | Mandatory  | Description     |
| ------- | --------------------------------------- | ---- | ------- |
| options | [ActionMenuOptions](#actionmenuoptions) | Yes   | Action menu options.|
W
wusongqing 已提交
220 221

**Return value**
G
Gloria 已提交
222

W
wusongqing 已提交
223 224 225
| Type                                      | Description     |
| ---------------------------------------- | ------- |
| Promise&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)&gt; | Promise used to return the action menu response result.|
W
wusongqing 已提交
226 227

**Example**
G
Gloria 已提交
228 229

```js
W
wusongqing 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
prompt.showActionMenu({
  title: 'showActionMenu Title Info',
  buttons: [
    {
      text: 'item1',
      color: '#666666',
    },
    {
      text: 'item2',
      color: '#000000',
    },
  ]
})
  .then(data => {
    console.info('showActionMenu success, click button: ' + data.index);
  })
  .catch(err => {
    console.info('showActionMenu error: ' + err);
  })
G
Gloria 已提交
249
```
250 251
![en-us_image_0006](figures/en-us_image_0006.gif)

W
wusongqing 已提交
252 253 254 255 256 257
## ActionMenuOptions

Describes the options for showing the action menu.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

E
ester.zhou 已提交
258 259 260 261
| Name   | Type                                                        | Mandatory| Description                                                        |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | No  | Title of the text to display.                                                  |
| 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, extra buttons will not be displayed.|
W
wusongqing 已提交
262 263 264 265 266 267 268

## ActionMenuSuccessResponse

Describes the action menu response result.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
269 270
| Name   | Type    | Mandatory  | Description                      |
| ----- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
271 272 273 274 275 276 277 278 279 280
| index | number | No   | Index of the selected button in the **buttons** array, starting from **0**.|

## Button

Describes the menu item button in the action menu.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

| Name   | Type                                      | Mandatory  | Description     |
| ----- | ---------------------------------------- | ---- | ------- |
281 282
| text  | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | Yes   | Button text.|
| color | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | Yes   | Text color of the button.|