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