js-apis-prompt.md 9.2 KB
Newer Older
1
# @ohos.prompt (弹窗)
K
kukixi 已提交
2

T
explain  
tianyu 已提交
3 4
创建并显示文本提示框、对话框和操作菜单。

5
> **说明:**
Z
zhaoxinyu 已提交
6 7
> 从API Version 9 开始,该接口不再维护,推荐使用新接口[@ohos.promptAction (弹窗)](js-apis-promptAction.md)
> 
K
kukixi 已提交
8 9 10 11
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

H
HelloCrease 已提交
12
```js
K
kukixi 已提交
13 14 15 16 17 18 19 20 21 22 23 24
import prompt from '@ohos.prompt'
```

## prompt.showToast

showToast(options: ShowToastOptions): void

创建并显示文本提示框。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
H
HelloCrease 已提交
25

H
HelloCrease 已提交
26 27 28
| 参数名     | 类型                                    | 必填   | 说明      |
| ------- | ------------------------------------- | ---- | ------- |
| options | [ShowToastOptions](#showtoastoptions) | 是    | 文本弹窗选项。 |
K
kukixi 已提交
29 30

**示例:**
H
HelloCrease 已提交
31 32

```js
H
HelloCrease 已提交
33 34 35 36
prompt.showToast({            
  message: 'Message Info',
    duration: 2000,      
});
H
HelloCrease 已提交
37 38
```

W
wangshuainan 已提交
39 40
![zh-cn_image_0001](figures/zh-cn_image_0001.gif)

K
kukixi 已提交
41 42 43 44
## ShowToastOptions

文本提示框的选项。

H
HelloCrease 已提交
45
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full。
K
kukixi 已提交
46

H
HelloCrease 已提交
47 48
| 名称       | 类型                                       | 必填   | 说明                                       |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
49
| message  | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 是    | 显示的文本信息。                                 |
H
HelloCrease 已提交
50
| duration | number                                   | 否    | 默认值1500ms,取值区间:1500ms-10000ms。若小于1500ms则取默认值,若大于10000ms则取上限值10000ms。 |
L
lijuan124 已提交
51
| bottom   | string\| number                          | 否    | 设置弹窗边框距离屏幕底部的位置,无上限值,默认单位vp。                |
K
kukixi 已提交
52 53 54

## prompt.showDialog

K
kukixi 已提交
55
showDialog(options: ShowDialogOptions): Promise&lt;ShowDialogSuccessResponse&gt;
K
kukixi 已提交
56 57

创建并显示对话框,对话框响应后同步返回结果。
K
kukixi 已提交
58

H
HelloCrease 已提交
59
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
K
kukixi 已提交
60 61

**参数:**
H
HelloCrease 已提交
62

H
HelloCrease 已提交
63 64 65
| 参数名     | 类型                                      | 必填   | 说明     |
| ------- | --------------------------------------- | ---- | ------ |
| options | [ShowDialogOptions](#showdialogoptions) | 是    | 对话框选项。 |
K
kukixi 已提交
66 67

**返回值:**
K
kukixi 已提交
68

H
HelloCrease 已提交
69 70 71
| 类型                                       | 说明       |
| ---------------------------------------- | -------- |
| Promise&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | 对话框响应结果。 |
K
kukixi 已提交
72

K
kukixi 已提交
73
**示例:**
K
kukixi 已提交
74

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

W
wangshuainan 已提交
98 99
![zh-cn_image_0002](figures/zh-cn_image_0002.gif)

K
kukixi 已提交
100 101
## prompt.showDialog

K
kukixi 已提交
102
showDialog(options: ShowDialogOptions, callback: AsyncCallback&lt;ShowDialogSuccessResponse&gt;):void 
K
kukixi 已提交
103 104 105

创建并显示对话框,对话框响应结果异步返回。

H
HelloCrease 已提交
106
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
K
kukixi 已提交
107 108

**参数:**
H
HelloCrease 已提交
109

H
HelloCrease 已提交
110 111 112 113
| 参数名      | 类型                                       | 必填   | 说明           |
| -------- | ---------------------------------------- | ---- | ------------ |
| options  | [ShowDialogOptions](#showdialogoptions)  | 是    | 页面显示对话框信息描述。 |
| callback | AsyncCallback&lt;[ShowDialogSuccessResponse](#showdialogsuccessresponse)&gt; | 是    | 对话框响应结果回调。   |
K
kukixi 已提交
114 115

**示例:**
H
HelloCrease 已提交
116 117

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

W
wangshuainan 已提交
140 141
![zh-cn_image_0004](figures/zh-cn_image_0004.gif)

K
kukixi 已提交
142 143 144 145
## ShowDialogOptions

对话框的选项。

H
HelloCrease 已提交
146
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
K
kukixi 已提交
147

Y
yamila 已提交
148 149 150 151 152
| 名称    | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 否   | 标题文本。                                                   |
| message | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 否   | 内容文本。                                                   |
| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?]    | 否   | 对话框中按钮的数组,结构为:{text:'button',&nbsp;color:&nbsp;'\#666666'},支持1-3个按钮。其中第一个为positiveButton;第二个为negativeButton;第三个为neutralButton。 |
K
kukixi 已提交
153 154 155 156 157

## ShowDialogSuccessResponse 

对话框的响应结果。

H
HelloCrease 已提交
158
**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
K
kukixi 已提交
159

Y
yamila 已提交
160 161 162
| 名称  | 类型   | 必填 | 说明                            |
| ----- | ------ | ---- | ------------------------------- |
| index | number | 否   | 选中按钮在buttons数组中的索引。 |
K
kukixi 已提交
163 164 165 166


## prompt.showActionMenu

K
kukixi 已提交
167
showActionMenu(options: ActionMenuOptions, callback: AsyncCallback&lt;ActionMenuSuccessResponse&gt;):void
K
kukixi 已提交
168 169 170 171 172 173

创建并显示操作菜单,菜单响应结果异步返回。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full。

**参数:**
H
HelloCrease 已提交
174

H
HelloCrease 已提交
175 176 177 178
| 参数名      | 类型                                       | 必填   | 说明        |
| -------- | ---------------------------------------- | ---- | --------- |
| options  | [ActionMenuOptions](#actionmenuoptions)  | 是    | 操作菜单选项。   |
| callback | AsyncCallback&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | 是    | 菜单响应结果回调。 |
K
kukixi 已提交
179 180

**示例:**
H
HelloCrease 已提交
181 182

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

W
wangshuainan 已提交
204 205
![zh-cn_image_0005](figures/zh-cn_image_0005.gif)

K
kukixi 已提交
206
## prompt.showActionMenu
K
kukixi 已提交
207

L
luoying_ace_admin 已提交
208
showActionMenu(options: ActionMenuOptions): Promise&lt;ActionMenuSuccessResponse&gt;
K
kukixi 已提交
209 210 211 212 213 214

创建并显示操作菜单,菜单响应后同步返回结果。

**系统能力:** SystemCapability.ArkUI.ArkUI.Full

**参数:**
H
HelloCrease 已提交
215

H
HelloCrease 已提交
216 217 218
| 参数名     | 类型                                      | 必填   | 说明      |
| ------- | --------------------------------------- | ---- | ------- |
| options | [ActionMenuOptions](#actionmenuoptions) | 是    | 操作菜单选项。 |
K
kukixi 已提交
219

K
kukixi 已提交
220
**返回值:**
H
HelloCrease 已提交
221

H
HelloCrease 已提交
222 223 224
| 类型                                       | 说明      |
| ---------------------------------------- | ------- |
| Promise&lt;[ActionMenuSuccessResponse](#actionmenusuccessresponse)&gt; | 菜单响应结果。 |
K
kukixi 已提交
225 226

**示例:**
H
HelloCrease 已提交
227 228

```js
H
HelloCrease 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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);
  })
H
HelloCrease 已提交
248
```
W
wangshuainan 已提交
249 250
![zh-cn_image_0006](figures/zh-cn_image_0006.gif)

K
kukixi 已提交
251 252 253 254
## ActionMenuOptions

操作菜单的选项。

H
HelloCrease 已提交
255
**系统能力:** SystemCapability.ArkUI.ArkUI.Full。
K
kukixi 已提交
256

Y
yamila 已提交
257 258 259 260
| 名称    | 类型                                                         | 必填 | 说明                                                         |
| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| title   | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 否   | 标题文本。                                                   |
| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?] | 是   | 菜单中菜单项按钮的数组,结构为:{text:'button',&nbsp;color:&nbsp;'\#666666'},支持1-6个按钮。大于6个按钮时弹窗不显示。 |
K
kukixi 已提交
261 262 263 264 265

## ActionMenuSuccessResponse

操作菜单的响应结果。

H
HelloCrease 已提交
266
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
K
kukixi 已提交
267

H
HelloCrease 已提交
268 269 270
| 名称    | 类型     | 必填   | 说明                       |
| ----- | ------ | ---- | ------------------------ |
| index | number | 否    | 选中按钮在buttons数组中的索引,从0开始。 |
K
kukixi 已提交
271

L
luoying_ace_admin 已提交
272 273 274 275
## Button

菜单中的菜单项按钮。

H
HelloCrease 已提交
276
**系统能力:** SystemCapability.ArkUI.ArkUI.Full
L
luoying_ace_admin 已提交
277

H
HelloCrease 已提交
278 279
| 名称    | 类型                                       | 必填   | 说明      |
| ----- | ---------------------------------------- | ---- | ------- |
280 281
| text  | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 是    | 按钮文本内容。 |
| color | string\| [Resource](../arkui-ts/ts-types.md#resource)<sup>9+</sup> | 是    | 按钮文本颜色。 |
L
luoying_ace_admin 已提交
282