• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 弹窗
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
4>
5> - 从API Version 8 开始,该接口不再维护,推荐使用新接口[`@ohos.prompt`](js-apis-prompt.md)。
6>
7>
8> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9
10
11## 导入模块
12
13
14```js
15import prompt from '@system.prompt';
16```
17
18## prompt.showToast
19
20showToast(options: ShowToastOptions): void
21
22显示文本弹窗。
23
24**系统能力:** SystemCapability.ArkUI.ArkUI.Full
25
26**参数:**
27
28| 参数名     | 类型                                    | 必填   | 说明              |
29| ------- | ------------------------------------- | ---- | --------------- |
30| options | [ShowToastOptions](#showtoastoptions) | 是    | 定义ShowToast的选项。 |
31
32**示例:**
33
34```js
35export default {
36  showToast() {
37    prompt.showToast({
38      message: 'Message Info',
39      duration: 2000,
40    });
41  }
42}
43```
44
45
46## prompt.showDialog
47
48showDialog(options: ShowDialogOptions): void
49
50显示对话框。
51
52**系统能力:** SystemCapability.ArkUI.ArkUI.Full
53
54**参数:**
55
56| 参数名     | 类型                                      | 必填   | 说明          |
57| ------- | --------------------------------------- | ---- | ----------- |
58| options | [ShowDialogOptions](#showdialogoptions) | 是    | 定义显示对话框的选项。 |
59
60
61**示例:**
62
63```js
64export default {
65  showDialog() {
66    prompt.showDialog({
67      title: 'Title Info',
68      message: 'Message Info',
69      buttons: [
70        {
71           text: 'button',
72           color: '#666666',
73         },
74       ],
75       success: function(data) {
76         console.log('dialog success callback,click button : ' + data.index);
77       },
78       cancel: function() {
79         console.log('dialog cancel callback');
80       },
81     });
82  }
83}
84```
85
86## prompt.showActionMenu<sup>6+</sup>
87
88showActionMenu(options: ShowActionMenuOptions): void
89
90显示操作菜单。
91
92**系统能力:** SystemCapability.ArkUI.ArkUI.Full
93
94**参数:**
95
96| 参数名     | 类型                                       | 必填   | 说明                   |
97| ------- | ---------------------------------------- | ---- | -------------------- |
98| options | [ShowActionMenuOptions](#showactionmenuoptions) | 是    | 定义ShowActionMenu的选项。 |
99
100
101**示例:**
102
103```js
104export default {
105  showActionMenu() {
106    prompt.showActionMenu({
107      title: 'Title Info',
108      buttons: [
109        {
110          text: 'item1',
111          color: '#666666',
112        },
113        {
114           text: 'item2',
115           color: '#000000',
116        },
117      ],
118      success: function(tapIndex) {
119        console.log('dialog success callback,click button : ' + tapIndex);
120      },
121      fail: function(errMsg) {
122        console.log('dialog fail callback' + errMsg);
123      },
124    });
125  }
126}
127```
128## ShowToastOptions
129
130定义ShowToast的选项。
131
132**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full
133
134| 名称                  | 参数类型           | 必填   | 说明                                       |
135| ------------------- | -------------- | ---- | ---------------------------------------- |
136| message             | string         | 是    | 显示的文本信息。                                 |
137| duration            | number         | 否    | 默认值1500ms,建议区间:1500ms-10000ms。若小于1500ms则取默认值,最大取值为10000ms。 |
138| bottom<sup>5+</sup> | string\|number | 否    | 设置弹窗边框距离屏幕底部的位置。                         |
139
140## Button
141
142定义按钮的提示信息。
143
144**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full
145
146| 名称    | 参数类型   | 必填   | 说明      |
147| ----- | ------ | ---- | ------- |
148| text  | string | 是    | 定义按钮信息。 |
149| color | string | 是    | 定义按钮颜色。 |
150
151## ShowDialogSuccessResponse
152
153定义ShowDialog的响应。
154
155**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full
156
157| 名称    | 参数类型   | 必填   | 说明         |
158| ----- | ------ | ---- | ---------- |
159| index | number | 是    | 定义数据的索引信息。 |
160
161## ShowDialogOptions
162
163定义显示对话框的选项。
164
165**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full
166
167| 名称       | 参数类型                                     | 必填   | 说明                                       |
168| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
169| title    | string                                   | 否    | 标题文本。                                    |
170| message  | string                                   | 否    | 文本内容。                                    |
171| buttons  | [[Button](#button), [Button](#button)?, [Button](#button)?] | 否    | 对话框中按钮的数组,结构为:{text:'button', color: '\#666666'},支持1-6个按钮。大于6个按钮时弹窗不显示。 |
172| success  | (data: [ShowDialogSuccessResponse](#showdialogsuccessresponse)) => void | 否    | 接口调用成功的回调函数。                             |
173| cancel   | (data: string, code: string) => void     | 否    | 接口调用失败的回调函数。                             |
174| complete | (data: string) => void                   | 否    | 接口调用结束的回调函数。                             |
175
176## ShowActionMenuOptions<sup>6+</sup>
177
178定义ShowActionMenu的选项。
179
180**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full
181
182| 名称       | 参数类型                                     | 必填   | 说明                                       |
183| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
184| title    | string                                   | 否    | 标题文本。                                    |
185| buttons  | [[Button](#button), [Button](#button)?, [Button](#button)?, [Button](#button)?, [Button](#button)?, [Button](#button)?] | 是    | 对话框中按钮的数组,结构为:{text:'button', color: '\#666666'},支持1-6个按钮。 |
186| success  | (tapIndex: number, errMsg: string) => void | 否    | 弹出对话框时调用。                                |
187| fail     | (errMsg: string) => void                 | 否    | 接口调用失败的回调函数。                             |
188| complete | (data: string) => void                   | 否    | 关闭对话框时调用。                                |
189