• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 列表选择弹窗 (ActionSheet)
2
3列表弹窗。
4
5>  **说明:**
6>
7>  从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。
10>
11> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showActionSheet](../js-apis-arkui-UIContext.md#showactionsheet)来明确UI的执行上下文。
12
13## ActionSheet
14
15### show
16
17static show(value: ActionSheetOptions)
18
19定义列表弹窗并弹出。
20
21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
22
23**系统能力:** SystemCapability.ArkUI.ArkUI.Full
24
25**参数:**
26
27| 参数名 | 类型                                              | 必填 | 说明                     |
28| ------ | ------------------------------------------------- | ---- | ------------------------ |
29| value  | [ActionSheetOptions](#actionsheetoptions对象说明) | 是   | 配置列表选择弹窗的参数。 |
30
31## ActionSheetOptions对象说明
32
33**系统能力:** SystemCapability.ArkUI.ArkUI.Full
34
35| 名称      | 类型                    | 必填  | 说明                          |
36| ---------- | -------------------------- | ------- | ----------------------------- |
37| title      | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 是     |  弹窗标题。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
38| subtitle<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | 否 | 弹窗副标题。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
39| message    | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 是     | 弹窗内容。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。  |
40| autoCancel | boolean                           | 否     | 点击遮障层时,是否关闭弹窗。<br>默认值:true<br>值为true时,点击遮障层关闭弹窗,值为false时,点击遮障层不关闭弹窗。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
41| confirm    | {<br/>enabled<sup>10+</sup>?: boolean,<br/>defaultFocus<sup>10+</sup>?: boolean,<br />style<sup>10+</sup>?: [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10),<br />value:&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>action:&nbsp;()&nbsp;=&gt;&nbsp;void<br/>} | 否  | 确认Button的使能状态、默认焦点、按钮风格、文本内容和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。<br>enabled:点击Button是否响应,true表示Button可以响应,false表示Button不可以响应。<br />默认值:true<br />defaultFocus:设置Button是否是默认焦点,true表示Button是默认焦点,false表示Button不是默认焦点。<br />默认值:false<br />style:设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT<br/>value:Button文本内容。<br/>action:&nbsp;Button选中时的回调。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
42| cancel     | ()&nbsp;=&gt;&nbsp;void           | 否     | 点击遮障层关闭dialog时的回调。  <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。  |
43| alignment  | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否     |  弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Bottom  <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。<br/>**说明**:<br/>若在UIExtension中设置showInSubWindow为true, 弹窗将基于UIExtension的宿主窗口对齐。|
44| offset     | {<br/>dx:&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource),<br/>dy:&nbsp;number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource)<br/>} | 否      | 弹窗相对alignment所在位置的偏移量。<br/>默认值:<br/>1.alignment设置为Top、TopStart、TopEnd时默认值为{dx:&nbsp;0,dy:&nbsp;"40vp"} <br/>2.alignment设置为其他时默认值为{dx:&nbsp;0,dy:&nbsp;"-40vp"} <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
45| sheets     | Array&lt;[SheetInfo](#sheetinfo对象说明)&gt; | 是       | 设置选项内容,每个选择项支持设置图片、文本和选中的回调。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
46| maskRect<sup>10+</sup> | [Rectangle](ts-methods-alert-dialog-box.md#rectangle8类型说明) | 否     | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。<br/>默认值:{ x: 0, y: 0, width: '100%', height: '100%' } <br/>**说明:**<br/>showInSubWindow为true时,maskRect不生效。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
47| showInSubWindow<sup>11+</sup> | boolean | 否 | 某弹框需要显示在主窗口之外时,是否在子窗口显示此弹窗。<br/>默认值:false,弹窗显示在应用内,而非独立子窗口。<br/>**说明**:showInSubWindow为true的弹窗无法触发显示另一个showInSubWindow为true的弹窗。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
48| isModal<sup>11+</sup> | boolean | 否 | 弹窗是否为模态窗口,模态窗口有蒙层,非模态窗口无蒙层。<br/>默认值:true,此时弹窗有蒙层。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
49| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
50| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | 否 | 弹窗背板模糊材质。<br/>默认值:BlurStyle.COMPONENT_ULTRA_THICK<br/>**说明:** <br/>设置为BlurStyle.NONE即可关闭背景虚化。当设置了backgroundBlurStyle为非NONE值时,则不要设置backgroundColor,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
51| onWillDismiss<sup>12+</sup> | Callback<[DismissDialogAction](#dismissdialogaction12)> | 否 | 交互式关闭回调函数。<br/>**说明:**<br/>1.当用户执行点击遮障层关闭、左滑/右滑、三键back、键盘ESC关闭交互操作时,如果注册该回调函数,则不会立刻关闭弹窗。在回调函数中可以通过reason得到阻拦关闭弹窗的操作类型,从而根据原因选择是否能关闭弹窗。当前组件返回的reason中,暂不支持CLOSE_BUTTON的枚举值。<br/>2.在onWillDismiss回调中,不能再做onWillDismiss拦截。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
52| cornerRadius<sup>12+</sup> | [Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[BorderRadiuses](ts-types.md#borderradiuses9) &nbsp;\|&nbsp; [LocalizedBorderRadiuses](ts-types.md#LocalizedBorderRadiuses12) | 否 | 设置背板的圆角半径。<br />可分别设置4个圆角的半径。<br />默认值:{ topLeft: '32vp', topRight: '32vp', bottomLeft: '32vp', bottomRight: '32vp' }<br /> 圆角大小受组件尺寸限制,最大值为组件宽或高的一半,若值为负,则按照默认值处理。 <br /> 百分比参数方式:以父元素弹窗宽和高的百分比来设置弹窗的圆角。<br/>**说明:**<br/>当cornerRadius属性类型为LocalizedBorderRadiuses时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
53| borderWidth<sup>12+</sup> | [Dimension](ts-types.md#dimension10)&nbsp;\|&nbsp;[EdgeWidths](ts-types.md#edgewidths9)&nbsp;\|&nbsp;[LocalizedEdgeWidths](ts-types.md#LocalizedEdgeWidths12) | 否 | 设置弹窗背板的边框宽度。<br />可分别设置4个边框宽度。<br />默认值:0<br /> 百分比参数方式:以父元素弹窗宽的百分比来设置弹窗的边框宽度。<br />当弹窗左边框和右边框大于弹窗宽度,弹窗上边框和下边框大于弹窗高度,显示可能不符合预期。<br/>**说明:**<br/>当borderWidth属性类型为LocalizedEdgeWidths时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
54| borderColor<sup>12+</sup> | [ResourceColor](ts-types.md#resourcecolor)&nbsp;\|&nbsp;[EdgeColors](ts-types.md#edgecolors9)&nbsp;\|&nbsp;[LocalizedEdgeColors](ts-types.md#LocalizedEdgeColors12) | 否 | 设置弹窗背板的边框颜色。<br/>默认值:Color.Black<br/> 如果使用borderColor属性,需要和borderWidth属性一起使用。<br/>**说明:**<br/>当borderColor属性类型为LocalizedEdgeColors时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
55| borderStyle<sup>12+</sup> | [BorderStyle](ts-appendix-enums.md#borderstyle)&nbsp;\|&nbsp;[EdgeStyles](ts-types.md#edgestyles9)  | 否 | 设置弹窗背板的边框样式。<br/>默认值:BorderStyle.Solid。<br/> 如果使用borderStyle属性,需要和borderWidth属性一起使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
56| width<sup>12+</sup> | [Dimension](ts-types.md#dimension10)   | 否 | 设置弹窗背板的宽度。<br />**说明:**<br>- 弹窗宽度默认最大值:400vp。<br />- 百分比参数方式:弹窗参考宽度为所在窗口的宽度,在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
57| height<sup>12+</sup> | [Dimension](ts-types.md#dimension10)   | 否 | 设置弹窗背板的高度。<br />**说明:**<br />- 弹窗高度默认最大值:0.9 *(窗口高度 - 安全区域)。<br />- 百分比参数方式:弹窗参考高度为(窗口高度 - 安全区域),在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
58| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明)&nbsp;\|&nbsp;[ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明)   | 否 | 设置弹窗背板的阴影。 <br /> 当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
59| transition<sup>12+</sup> | [TransitionEffect](ts-transition-animation-component.md#transitioneffect10) | 否 | 设置弹窗显示和退出的过渡效果。<br/>**说明:**<br/>1.如果不设置,则使用默认的显示/退出动效。<br/>2.显示动效中按back键,打断显示动效,执行退出动效,动画效果为显示动效与退出动效的曲线叠加后的效果。<br/>3.退出动效中按back键,不会打断退出动效,退出动效继续执行,继续按back键退出应用。 <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
60
61## SheetInfo对象说明
62
63**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
64
65**系统能力:** SystemCapability.ArkUI.ArkUI.Full
66
67| 名称 | 类型                                                     | 必填 | 说明         |
68| ------ | ------------------------------------------------------------ | ---- | ----------------- |
69| title  | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 是   | 选项的文本内容。       |
70| icon   | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 否   | 选项的图标,默认无图标显示。     |
71| action | ()=&gt;void                                          | 是   | 选项选中的回调。 |
72
73## DismissDialogAction<sup>12+</sup>
74
75Dialog关闭的信息。
76
77**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
78
79**系统能力:** SystemCapability.ArkUI.ArkUI.Full
80
81### 属性
82
83| 名称    | 类型                                                         | 可读 | 可写 | 说明                                                         |
84| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
85| dismiss | Callback&lt;void&gt;                                         | 否   | 否   | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 |
86| reason  | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否   | 否   | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 |
87
88## 示例
89
90>  **说明:**
91>
92> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showActionSheet](../js-apis-arkui-UIContext.md#showactionsheet)来明确UI的执行上下文。
93
94### 示例1
95
96```ts
97@Entry
98@Component
99struct ActionSheetExample {
100  build() {
101    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
102      Button('Click to Show ActionSheet')
103        .onClick(() => {
104          ActionSheet.show({ // 建议使用 this.getUIContext().showActionSheet()接口
105            title: 'ActionSheet title',
106            subtitle: 'ActionSheet subtitle',
107            message: 'message',
108            autoCancel: true,
109            confirm: {
110              defaultFocus: true,
111              value: 'Confirm button',
112              action: () => {
113                console.log('Get Alert Dialog handled')
114              }
115            },
116            cancel: () => {
117              console.log('actionSheet canceled')
118            },
119              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
120                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
121                console.log("dialog onWillDismiss")
122                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
123                  dismissDialogAction.dismiss()
124                }
125                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
126                  dismissDialogAction.dismiss()
127                }
128              },
129            alignment: DialogAlignment.Bottom,
130            offset: { dx: 0, dy: -10 },
131            sheets: [
132              {
133                title: 'apples',
134                action: () => {
135                  console.log('apples')
136                }
137              },
138              {
139                title: 'bananas',
140                action: () => {
141                  console.log('bananas')
142                }
143              },
144              {
145                title: 'pears',
146                action: () => {
147                  console.log('pears')
148                }
149              }
150            ]
151          })
152        })
153    }.width('100%')
154    .height('100%')
155  }
156}
157```
158
159![zh-cn_image_action](figures/zh-cn_image_action.gif)
160
161### 示例2
162
163```ts
164@Entry
165@Component
166struct ActionSheetExample {
167  build() {
168    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
169      Button('Click to Show ActionSheet')
170        .onClick(() => {
171          ActionSheet.show({
172            title: 'ActionSheet title',
173            subtitle: 'ActionSheet subtitle',
174            message: 'message',
175            autoCancel: true,
176            showInSubWindow: true,
177            isModal: true,
178            confirm: {
179              defaultFocus: true,
180              value: 'Confirm button',
181              action: () => {
182                console.log('Get Alert Dialog handled')
183              }
184            },
185            cancel: () => {
186              console.log('actionSheet canceled')
187            },
188              onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
189                console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
190                console.log("dialog onWillDismiss")
191                if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
192                  dismissDialogAction.dismiss()
193                }
194                if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
195                  dismissDialogAction.dismiss()
196                }
197              },
198            alignment: DialogAlignment.Center,
199            offset: { dx: 0, dy: -10 },
200            sheets: [
201              {
202                title: 'apples',
203                action: () => {
204                  console.log('apples')
205                }
206              },
207              {
208                title: 'bananas',
209                action: () => {
210                  console.log('bananas')
211                }
212              },
213              {
214                title: 'pears',
215                action: () => {
216                  console.log('pears')
217                }
218              }
219            ]
220          })
221        })
222    }.width('100%')
223    .height('100%')
224  }
225}
226```
227
228![zh-cn_image_action_showinsubwindow](figures/zh-cn_image_action_showinsubwindow.jpg)
229
230### 示例3
231ActionSheet显示动画持续3秒,退出动画持续100毫秒
232```ts
233@Entry
234@Component
235struct ActionSheetExample {
236  build() {
237    Column({ space: 5 }) {
238      Button('ActionSheet Set Duration')
239        .onClick(() => {
240            ActionSheet.show({
241              title: 'ActionSheet 1',
242              message: 'Set Animation Duration open 3 second, close 100 ms',
243              autoCancel: true,
244              alignment: DialogAlignment.Top,
245              transition:TransitionEffect.asymmetric(TransitionEffect.OPACITY
246                .animation({ duration: 3000, curve: Curve.Sharp }).combine(TransitionEffect.scale({x: 1.5, y: 1.5}).animation({duration: 3000, curve: Curve.Sharp})),
247                TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth })
248                  .combine(TransitionEffect.scale({x: 0.5, y: 0.5}).animation({duration: 100, curve: Curve.Smooth}))),
249              offset: { dx: 0, dy: -20 },
250              confirm: {
251                value: 'button',
252                action: () => {
253                  console.info('Button-clicking callback')
254                }
255              },
256              cancel: () => {
257                console.info('Closed callbacks')
258              },
259              sheets: [
260                {
261                  title: 'apples',
262                  action: () => {
263                    console.log('apples')
264                  }
265                },
266                {
267                  title: 'bananas',
268                  action: () => {
269                    console.log('bananas')
270                  }
271                },
272                {
273                  title: 'pears',
274                  action: () => {
275                    console.log('pears')
276                  }
277                }
278              ]
279            })
280        }).backgroundColor(0x317aff).height("88px")
281    }.width('100%').margin({ top: 5 })
282  }
283}
284```
285
286![zh-cn_image_action_animation](figures/zh-cn_image_action_animation.gif)
287
288### 示例4
289该示例定义了ActionSheet的样式,如宽度、高度、背景色、阴影等等
290```ts
291@Entry
292@Component
293struct ActionSheetExample {
294  build() {
295    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
296      Button('Click to Show ActionSheet')
297        .onClick(() => {
298          ActionSheet.show({
299            title: 'ActionSheet title',
300            subtitle: 'ActionSheet subtitle',
301            message: 'message',
302            autoCancel: true,
303            width: 300,
304            height: 350,
305            cornerRadius: 20,
306            borderWidth: 1,
307            borderStyle: BorderStyle.Solid,//使用borderStyle属性,需要和borderWidth属性一起使用
308            borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
309            backgroundColor: Color.White,
310            shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
311            confirm: {
312              defaultFocus: true,
313              value: 'Confirm button',
314              action: () => {
315                console.log('Get Alert Dialog handled')
316              }
317            },
318            cancel: () => {
319              console.log('actionSheet canceled')
320            },
321            onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
322              console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
323              console.log("dialog onWillDismiss")
324              if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
325                dismissDialogAction.dismiss()
326              }
327              if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
328                dismissDialogAction.dismiss()
329              }
330            },
331            alignment: DialogAlignment.Bottom,
332            offset: { dx: 0, dy: -10 },
333            sheets: [
334              {
335                title: 'apples',
336                action: () => {
337                  console.log('apples')
338                }
339              },
340              {
341                title: 'bananas',
342                action: () => {
343                  console.log('bananas')
344                }
345              },
346              {
347                title: 'pears',
348                action: () => {
349                  console.log('pears')
350                }
351              }
352            ]
353          })
354        })
355    }.width('100%')
356    .height('100%')
357  }
358}
359```
360
361![zh-cn_image_action_style](figures/zh-cn_image_action_style.gif)
362