• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.prompt (弹窗)
2
3创建并显示文本提示框、对话框和操作菜单。
4
5> **说明:**
6> 从API Version 9 开始,该接口不再维护,推荐使用新接口[@ohos.promptAction (弹窗)](js-apis-promptAction.md)
7>
8> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9
10## 导入模块
11
12```ts
13import prompt from '@ohos.prompt'
14```
15
16## prompt.showToast
17
18showToast(options: ShowToastOptions): void
19
20创建并显示文本提示框。
21
22**系统能力:** SystemCapability.ArkUI.ArkUI.Full
23
24**参数:**
25
26| 参数名     | 类型                                    | 必填   | 说明      |
27| ------- | ------------------------------------- | ---- | ------- |
28| options | [ShowToastOptions](#showtoastoptions) | 是    | 文本弹窗选项。 |
29
30**示例:**
31
32```ts
33import prompt from '@ohos.prompt'
34prompt.showToast({
35  message: 'Message Info',
36    duration: 2000
37});
38```
39
40![zh-cn_image_0001](figures/zh-cn_image_0001.gif)
41
42## ShowToastOptions
43
44文本提示框的选项。
45
46**系统能力:**  SystemCapability.ArkUI.ArkUI.Full47
48| 名称     | 类型            | 必填 | 说明                                                         |
49| -------- | --------------- | ---- | ------------------------------------------------------------ |
50| message  | string          | 是   | 显示的文本信息。                                             |
51| duration | number          | 否   | 默认值1500ms,取值区间:1500ms-10000ms。若小于1500ms则取默认值,若大于10000ms则取上限值10000ms。 |
52| bottom   | string\| number | 否   | 设置弹窗边框距离屏幕底部的位置,无上限值,默认单位vp。       |
53
54## prompt.showDialog
55
56showDialog(options: ShowDialogOptions): Promise<ShowDialogSuccessResponse>
57
58创建并显示对话框,对话框响应后同步返回结果。
59
60**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
61
62**参数:**
63
64| 参数名     | 类型                                      | 必填   | 说明     |
65| ------- | --------------------------------------- | ---- | ------ |
66| options | [ShowDialogOptions](#showdialogoptions) | 是    | 对话框选项。 |
67
68**返回值:**
69
70| 类型                                       | 说明       |
71| ---------------------------------------- | -------- |
72| Promise<[ShowDialogSuccessResponse](#showdialogsuccessresponse)> | 对话框响应结果。 |
73
74**示例:**
75
76```ts
77import prompt from '@ohos.prompt'
78prompt.showDialog({
79  title: 'Title Info',
80  message: 'Message Info',
81  buttons: [
82    {
83      text: 'button1',
84      color: '#000000'
85    },
86    {
87      text: 'button2',
88      color: '#000000'
89    }
90  ],
91})
92  .then(data => {
93    console.info('showDialog success, click button: ' + data.index);
94  })
95  .catch((err:Error) => {
96    console.info('showDialog error: ' + err);
97  })
98```
99
100![zh-cn_image_0002](figures/zh-cn_image_0002.gif)
101
102## prompt.showDialog
103
104showDialog(options: ShowDialogOptions, callback: AsyncCallback<ShowDialogSuccessResponse>):void
105
106创建并显示对话框,对话框响应结果异步返回。
107
108**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
109
110**参数:**
111
112| 参数名      | 类型                                       | 必填   | 说明           |
113| -------- | ---------------------------------------- | ---- | ------------ |
114| options  | [ShowDialogOptions](#showdialogoptions)  | 是    | 页面显示对话框信息描述。 |
115| callback | AsyncCallback<[ShowDialogSuccessResponse](#showdialogsuccessresponse)> | 是    | 对话框响应结果回调。   |
116
117**示例:**
118
119```ts
120import prompt from '@ohos.prompt'
121prompt.showDialog({
122  title: 'showDialog Title Info',
123  message: 'Message Info',
124  buttons: [
125    {
126      text: 'button1',
127      color: '#000000'
128    },
129    {
130      text: 'button2',
131      color: '#000000'
132    }
133  ]
134}, (err, data) => {
135  if (err) {
136    console.info('showDialog err: ' + err);
137    return;
138  }
139  console.info('showDialog success callback, click button: ' + data.index);
140});
141```
142
143![zh-cn_image_0004](figures/zh-cn_image_0004.gif)
144
145## ShowDialogOptions
146
147对话框的选项。
148
149**系统能力:** SystemCapability.ArkUI.ArkUI.Full
150
151| 名称    | 类型                                                      | 必填 | 说明                                                         |
152| ------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
153| title   | string                                                    | 否   | 标题文本。                                                   |
154| message | string                                                    | 否   | 内容文本。                                                   |
155| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?] | 否   | 对话框中按钮的数组,结构为:{text:'button', color: '\#666666'},支持1-3个按钮。其中第一个为positiveButton;第二个为negativeButton;第三个为neutralButton。 |
156
157## ShowDialogSuccessResponse
158
159对话框的响应结果。
160
161**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
162
163| 名称  | 类型   | 必填 | 说明                            |
164| ----- | ------ | ---- | ------------------------------- |
165| index | number | 否   | 选中按钮在buttons数组中的索引。 |
166
167
168## prompt.showActionMenu
169
170showActionMenu(options: ActionMenuOptions, callback: AsyncCallback<ActionMenuSuccessResponse>):void
171
172创建并显示操作菜单,菜单响应结果异步返回。
173
174**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full175
176**参数:**
177
178| 参数名      | 类型                                       | 必填   | 说明        |
179| -------- | ---------------------------------------- | ---- | --------- |
180| options  | [ActionMenuOptions](#actionmenuoptions)  | 是    | 操作菜单选项。   |
181| callback | AsyncCallback<[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | 是    | 菜单响应结果回调。 |
182
183**示例:**
184
185```ts
186import prompt from '@ohos.prompt'
187prompt.showActionMenu({
188  title: 'Title Info',
189  buttons: [
190    {
191      text: 'item1',
192      color: '#666666'
193    },
194    {
195      text: 'item2',
196      color: '#000000'
197    },
198  ]
199}, (err, data) => {
200  if (err) {
201    console.info('showActionMenu err: ' + err);
202    return;
203  }
204  console.info('showActionMenu success callback, click button: ' + data.index);
205})
206```
207
208![zh-cn_image_0005](figures/zh-cn_image_0005.gif)
209
210## prompt.showActionMenu
211
212showActionMenu(options: ActionMenuOptions): Promise<ActionMenuSuccessResponse>
213
214创建并显示操作菜单,菜单响应后同步返回结果。
215
216**系统能力:** SystemCapability.ArkUI.ArkUI.Full
217
218**参数:**
219
220| 参数名     | 类型                                      | 必填   | 说明      |
221| ------- | --------------------------------------- | ---- | ------- |
222| options | [ActionMenuOptions](#actionmenuoptions) | 是    | 操作菜单选项。 |
223
224**返回值:**
225
226| 类型                                       | 说明      |
227| ---------------------------------------- | ------- |
228| Promise<[ActionMenuSuccessResponse](#actionmenusuccessresponse)> | 菜单响应结果。 |
229
230**示例:**
231
232```ts
233import prompt from '@ohos.prompt'
234prompt.showActionMenu({
235  title: 'showActionMenu Title Info',
236  buttons: [
237    {
238      text: 'item1',
239      color: '#666666'
240    },
241    {
242      text: 'item2',
243      color: '#000000'
244    },
245  ]
246})
247  .then(data => {
248    console.info('showActionMenu success, click button: ' + data.index);
249  })
250  .catch((err:Error) => {
251    console.info('showActionMenu error: ' + err);
252  })
253```
254![zh-cn_image_0006](figures/zh-cn_image_0006.gif)
255
256## ActionMenuOptions
257
258操作菜单的选项。
259
260**系统能力:** SystemCapability.ArkUI.ArkUI.Full261
262| 名称    | 类型                                                         | 必填 | 说明                                                         |
263| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
264| title   | string                                                       | 否   | 标题文本。                                                   |
265| buttons | [[Button](#button),[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?,[Button](#button)?] | 是   | 菜单中菜单项按钮的数组,结构为:{text:'button', color: '\#666666'},支持1-6个按钮。大于6个按钮时弹窗不显示。 |
266
267## ActionMenuSuccessResponse
268
269操作菜单的响应结果。
270
271**系统能力:** SystemCapability.ArkUI.ArkUI.Full
272
273| 名称    | 类型     | 必填   | 说明                       |
274| ----- | ------ | ---- | ------------------------ |
275| index | number | 否    | 选中按钮在buttons数组中的索引,从0开始。 |
276
277## Button
278
279菜单中的菜单项按钮。
280
281**系统能力:** SystemCapability.ArkUI.ArkUI.Full
282
283| 名称  | 类型   | 必填 | 说明           |
284| ----- | ------ | ---- | -------------- |
285| text  | string | 是   | 按钮文本内容。 |
286| color | string | 是   | 按钮文本颜色。 |
287
288