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