1# 列表选择弹窗 (ActionSheet) 2 3列表弹窗。 4 5> **说明:** 6> 7> 从API version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。 10 11## ActionSheetOptions对象说明 12 13**系统能力:** SystemCapability.ArkUI.ArkUI.Full 14 15| 名称 | 类型 | 必填 | 说明 | 16| ---------- | -------------------------- | ------- | ----------------------------- | 17| title | string \| [Resource](ts-types.md#resource) | 是 | 弹窗标题。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 18| subtitle<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | 否 | 弹窗副标题。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 19| message | string \| [Resource](ts-types.md#resource) | 是 | 弹窗内容。<br/>文本超长时会触发滚动条。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 20| autoCancel | boolean | 否 | 点击遮障层时,是否关闭弹窗。<br>默认值:true<br>值为true时,点击遮障层关闭弹窗,值为false时,点击遮障层不关闭弹窗。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 21| confirm | [ActionSheetButtonOptions](#actionsheetbuttonoptions18对象说明) | 否 | 确认Button的使能状态、默认焦点、按钮风格、文本内容和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 22| cancel | [VoidCallback](ts-types.md#voidcallback12) | 否 | 点击遮障层关闭dialog时的回调。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 23| alignment | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Bottom <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。<br/>**说明**:<br/>若在UIExtension中设置showInSubWindow为true, 弹窗将基于UIExtension的宿主窗口对齐。| 24| offset | [ActionSheetOffset](#actionsheetoffset18对象说明) | 否 | 弹窗相对alignment所在位置的偏移量。<br/>默认值:<br/>1.alignment设置为Top、TopStart、TopEnd时默认值为{dx: 0,dy: "40vp"} <br/>2.alignment设置为其他时默认值为{dx: 0,dy: "-40vp"} <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 25| sheets | Array<[SheetInfo](#sheetinfo对象说明)> | 是 | 设置选项内容,每个选择项支持设置图片、文本和选中的回调。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 26| 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开始,该接口支持在原子化服务中使用。| 27| showInSubWindow<sup>11+</sup> | boolean | 否 | 某弹框需要显示在主窗口之外时,是否在子窗口显示此弹窗。<br/>默认值:false,弹窗显示在应用内,而非独立子窗口。<br/>**说明**:showInSubWindow为true的弹窗无法触发显示另一个showInSubWindow为true的弹窗。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 28| isModal<sup>11+</sup> | boolean | 否 | 弹窗是否为模态窗口,模态窗口有蒙层,非模态窗口无蒙层。<br/>默认值:true,此时弹窗有蒙层。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 29| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 30| 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开始,该接口支持在原子化服务中使用。 | 31| 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开始,该接口支持在原子化服务中使用。 | 32| cornerRadius<sup>12+</sup> | [Dimension](ts-types.md#dimension10) \| [BorderRadiuses](ts-types.md#borderradiuses9) \| [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开始,该接口支持在原子化服务中使用。| 33| borderWidth<sup>12+</sup> | [Dimension](ts-types.md#dimension10) \| [EdgeWidths](ts-types.md#edgewidths9) \| [LocalizedEdgeWidths](ts-types.md#localizededgewidths12) | 否 | 设置弹窗背板的边框宽度。<br />可分别设置4个边框宽度。<br />默认值:0<br /> 百分比参数方式:以父元素弹窗宽的百分比来设置弹窗的边框宽度。<br />当弹窗左边框和右边框大于弹窗宽度,弹窗上边框和下边框大于弹窗高度,显示可能不符合预期。<br/>**说明:**<br/>当borderWidth属性类型为LocalizedEdgeWidths时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 34| borderColor<sup>12+</sup> | [ResourceColor](ts-types.md#resourcecolor) \| [EdgeColors](ts-types.md#edgecolors9) \| [LocalizedEdgeColors](ts-types.md#localizededgecolors12) | 否 | 设置弹窗背板的边框颜色。<br/>默认值:Color.Black<br/> 如果使用borderColor属性,需要和borderWidth属性一起使用。<br/>**说明:**<br/>当borderColor属性类型为LocalizedEdgeColors时,支持随语言习惯改变布局顺序。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 35| borderStyle<sup>12+</sup> | [BorderStyle](ts-appendix-enums.md#borderstyle) \| [EdgeStyles](ts-types.md#edgestyles9) | 否 | 设置弹窗背板的边框样式。<br/>默认值:BorderStyle.Solid。<br/> 如果使用borderStyle属性,需要和borderWidth属性一起使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 36| width<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | 否 | 设置弹窗背板的宽度。<br />**说明:**<br>- 弹窗宽度默认最大值:400vp。<br />- 百分比参数方式:弹窗参考宽度为所在窗口的宽度,在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 37| height<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | 否 | 设置弹窗背板的高度。<br />**说明:**<br />- 弹窗高度默认最大值:0.9 *(窗口高度 - 安全区域)。<br />- 百分比参数方式:弹窗参考高度为(窗口高度 - 安全区域),在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 38| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明) | 否 | 设置弹窗背板的阴影。 <br /> 当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 39| 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开始,该接口支持在原子化服务中使用。| 40| enableHoverMode<sup>14+</sup> | boolean | 否 | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 41| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否 | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 42| levelMode<sup>15+</sup> | [LevelMode](#levelmode15) | 否 | 设置弹窗显示层级。<br />**说明:**<br />- 默认值:LevelMode.OVERLAY。<br />- 当且仅当showInSubWindow属性设置为false时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 43| levelUniqueId<sup>15+</sup> | number | 否 | 设置页面级弹窗需要显示的层级下的[节点 uniqueId](../js-apis-arkui-frameNode.md#getuniqueid12)。<br />**说明:**<br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 44| immersiveMode<sup>15+</sup> | [ImmersiveMode](#immersivemode15) | 否 | 设置页面内弹窗蒙层效果。<br />**说明:**<br />- 默认值:ImmersiveMode.DEFAULT <br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 45| levelOrder<sup>18+</sup> | [LevelOrder](../js-apis-promptAction.md#levelorder18) | 否 | 设置弹窗显示的顺序。<br />**说明:**<br />- 默认值:LevelOrder.clamp(0) <br />- 不支持动态刷新顺序。<br/>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。| 46 47## SheetInfo对象说明 48 49**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 50 51**系统能力:** SystemCapability.ArkUI.ArkUI.Full 52 53| 名称 | 类型 | 必填 | 说明 | 54| ------ | ------------------------------------------------------------ | ---- | ----------------- | 55| title | string \| [Resource](ts-types.md#resource) | 是 | 选项的文本内容。<br/>文本超长时会触发滚动条。| 56| icon | string \| [Resource](ts-types.md#resource) | 否 | 选项的图标,默认无图标显示。<br>string格式可用于加载网络图片和本地图片,常用于加载网络图片。当使用相对路径引用本地图片时,例如Image("common/test.jpg")。| 57| action | [VoidCallback](ts-types.md#voidcallback12) | 是 | 选项选中的回调。 | 58 59## LevelMode<sup>15+</sup> 60 61type LevelMode = LevelMode 62 63弹窗的显示层级。 64 65**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 66 67**系统能力:** SystemCapability.ArkUI.ArkUI.Full 68 69| 类型 | 说明 | 70| ----------------------------------------------------------- | -------------------- | 71| [LevelMode](../js-apis-promptAction.md#levelmode15枚举说明) | 设置弹窗的显示层级。 | 72 73## ImmersiveMode<sup>15+</sup> 74 75type ImmersiveMode = ImmersiveMode 76 77弹窗的蒙层效果。 78 79**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 80 81**系统能力:** SystemCapability.ArkUI.ArkUI.Full 82 83| 类型 | 说明 | 84| ------------------------------------------------------------ | -------------------------- | 85| [ImmersiveMode](../js-apis-promptAction.md#immersivemode15枚举说明) | 设置页面内弹窗的蒙层效果。 | 86 87## DismissDialogAction<sup>12+</sup> 88 89Dialog关闭的信息。 90 91**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 92 93**系统能力:** SystemCapability.ArkUI.ArkUI.Full 94 95### 属性 96 97| 名称 | 类型 | 可读 | 可写 | 说明 | 98| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 99| dismiss | Callback<void> | 否 | 否 | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 | 100| reason | [DismissReason](../js-apis-promptAction.md#dismissreason12枚举说明) | 否 | 否 | Dialog无法关闭原因。根据开发者需要选择不同操作下,Dialog是否需要关闭。 | 101 102## ActionSheetButtonOptions<sup>18+</sup>对象说明 103 104**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 105 106**系统能力:** SystemCapability.ArkUI.ArkUI.Full 107 108| 名称 | 类型 | 必填 | 说明 | 109| ------------ | ------- | ---- | ---- | 110| enabled<sup>10+</sup> | boolean | 否 | 点击Button是否响应,true表示Button可以响应,false表示Button不可以响应。<br/>默认值:true<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 111| defaultFocus<sup>10+</sup> | boolean | 否 | 设置Button是否是默认焦点,true表示Button是默认焦点,false表示Button不是默认焦点。<br/>默认值:false<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 112| style<sup>10+</sup> | [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10) | 否 | 设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 113| value<sup>8+</sup> | string \| [Resource](ts-types.md#resource) | 是 | Button文本内容。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 114| action<sup>8+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 是 | Button选中时的回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 115 116## ActionSheetOffset<sup>18+</sup>对象说明 117 118**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 119 120**系统能力:** SystemCapability.ArkUI.ArkUI.Full 121 122| 名称 | 类型 | 必填 | 说明 | 123| ---- | ------------------------------------------------------------ | ---- | ---------------------------------- | 124| dx | number \| string \| [Resource](ts-types.md#resource) | 是 | 弹出窗口相对于对齐位置dx的偏移量。<br/>需要显式指定像素单位,如'10px',也可设置百分比字符串,如'100%'。<br/>**说明:** <br/>不指定像素单位时,默认单位vp,如'10',等同于10。 | 125| dy | number \| string \| [Resource](ts-types.md#resource) | 是 | 弹出窗口相对于对齐位置dy的偏移量。<br/>需要显式指定像素单位,如'10px',也可设置百分比字符串,如'100%'。<br/>**说明:** <br/>不指定像素单位时,默认单位vp,如'10',等同于10。| 126 127## ActionSheet 128 129### show<sup>(deprecated)</sup> 130 131static show(value: ActionSheetOptions) 132 133定义列表弹窗并弹出。 134 135> **说明:** 136> 137> 从API version 18开始废弃,建议使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showActionSheet](../js-apis-arkui-UIContext.md#showactionsheet)替代。 138> 139> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showActionSheet](../js-apis-arkui-UIContext.md#showactionsheet)来明确UI的执行上下文。 140 141**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 142 143**系统能力:** SystemCapability.ArkUI.ArkUI.Full 144 145**参数:** 146 147| 参数名 | 类型 | 必填 | 说明 | 148| ------ | ------------------------------------------------- | ---- | ------------------------ | 149| value | [ActionSheetOptions](#actionsheetoptions对象说明) | 是 | 配置列表选择弹窗的参数。 | 150 151## 示例 152 153> **说明:** 154> 155> 直接使用ActionSheet可能导致实例不明确的问题,建议使用[getUIContext](../js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[showActionSheet](../js-apis-arkui-UIContext.md#showactionsheet)调用绑定实例的ActionSheet.show()。 156 157### 示例1(弹出列表选择弹窗) 158 159该示例通过点击按钮弹窗列表选择弹窗。 160 161```ts 162// xxx.ets 163@Entry 164@Component 165struct ActionSheetExample { 166 build() { 167 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 168 Button('Click to Show ActionSheet') 169 .onClick(() => { 170 this.getUIContext().showActionSheet({ 171 title: 'ActionSheet title', 172 subtitle: 'ActionSheet subtitle', 173 message: 'message', 174 autoCancel: true, 175 confirm: { 176 defaultFocus: true, 177 value: 'Confirm button', 178 action: () => { 179 console.log('Get Alert Dialog handled'); 180 } 181 }, 182 cancel: () => { 183 console.log('actionSheet canceled'); 184 }, 185 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 186 console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 187 console.log("dialog onWillDismiss"); 188 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 189 dismissDialogAction.dismiss(); 190 } 191 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 192 dismissDialogAction.dismiss(); 193 } 194 }, 195 alignment: DialogAlignment.Bottom, 196 offset: { dx: 0, dy: -10 }, 197 sheets: [ 198 { 199 title: 'apples', 200 action: () => { 201 console.log('apples'); 202 } 203 }, 204 { 205 title: 'bananas', 206 action: () => { 207 console.log('bananas'); 208 } 209 }, 210 { 211 title: 'pears', 212 action: () => { 213 console.log('pears'); 214 } 215 } 216 ] 217 }) 218 }) 219 }.width('100%') 220 .height('100%') 221 } 222} 223``` 224 225 226 227### 示例2(可在主窗外弹出的弹窗) 228 229在2in1设备上设置showInSubWindow为true时,可以弹出在主窗外显示的弹窗。 230 231```ts 232// xxx.ets 233@Entry 234@Component 235struct ActionSheetExample { 236 build() { 237 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 238 Button('Click to Show ActionSheet') 239 .onClick(() => { 240 this.getUIContext().showActionSheet({ 241 title: 'ActionSheet title', 242 subtitle: 'ActionSheet subtitle', 243 message: 'message', 244 autoCancel: true, 245 showInSubWindow: true, 246 isModal: true, 247 confirm: { 248 defaultFocus: true, 249 value: 'Confirm button', 250 action: () => { 251 console.log('Get Alert Dialog handled'); 252 } 253 }, 254 cancel: () => { 255 console.log('actionSheet canceled'); 256 }, 257 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 258 console.info("reason=" + JSON.stringify(dismissDialogAction.reason)); 259 console.log("dialog onWillDismiss"); 260 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 261 dismissDialogAction.dismiss(); 262 } 263 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 264 dismissDialogAction.dismiss(); 265 } 266 }, 267 alignment: DialogAlignment.Center, 268 offset: { dx: 0, dy: -10 }, 269 sheets: [ 270 { 271 title: 'apples', 272 action: () => { 273 console.log('apples'); 274 } 275 }, 276 { 277 title: 'bananas', 278 action: () => { 279 console.log('bananas'); 280 } 281 }, 282 { 283 title: 'pears', 284 action: () => { 285 console.log('pears'); 286 } 287 } 288 ] 289 }) 290 }) 291 }.width('100%') 292 .height('100%') 293 } 294} 295``` 296 297 298 299### 示例3(设置弹窗的动画) 300 301该示例通过配置transition实现弹窗的显示和消失动画。 302 303```ts 304// xxx.ets 305@Entry 306@Component 307struct ActionSheetExample { 308 build() { 309 Column({ space: 5 }) { 310 Button('ActionSheet Set Duration') 311 .onClick(() => { 312 this.getUIContext().showActionSheet({ 313 title: 'ActionSheet 1', 314 message: 'Set Animation Duration open 3 second, close 100 ms', 315 autoCancel: true, 316 alignment: DialogAlignment.Top, 317 transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY 318 .animation({ duration: 3000, curve: Curve.Sharp }) 319 .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 }).animation({ duration: 3000, curve: Curve.Sharp })), 320 TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) 321 .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 }).animation({ duration: 100, curve: Curve.Smooth }))), 322 offset: { dx: 0, dy: -20 }, 323 confirm: { 324 value: 'button', 325 action: () => { 326 console.info('Button-clicking callback'); 327 } 328 }, 329 cancel: () => { 330 console.info('Closed callbacks'); 331 }, 332 sheets: [ 333 { 334 title: 'apples', 335 action: () => { 336 console.log('apples'); 337 } 338 }, 339 { 340 title: 'bananas', 341 action: () => { 342 console.log('bananas'); 343 } 344 }, 345 { 346 title: 'pears', 347 action: () => { 348 console.log('pears'); 349 } 350 } 351 ] 352 }) 353 }).backgroundColor(0x317aff).height("88px") 354 }.width('100%').margin({ top: 5 }) 355 } 356} 357``` 358 359 360 361### 示例4(设置弹窗的样式) 362 363该示例定义了ActionSheet的样式,如宽度、高度、背景色、阴影等等。 364 365```ts 366// xxx.ets 367@Entry 368@Component 369struct ActionSheetExample { 370 build() { 371 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 372 Button('Click to Show ActionSheet') 373 .onClick(() => { 374 this.getUIContext().showActionSheet({ 375 title: 'ActionSheet title', 376 subtitle: 'ActionSheet subtitle', 377 message: 'message', 378 autoCancel: true, 379 width: 300, 380 height: 350, 381 cornerRadius: 20, 382 borderWidth: 1, 383 borderStyle: BorderStyle.Solid, //使用borderStyle属性,需要和borderWidth属性一起使用 384 borderColor: Color.Blue, //使用borderColor属性,需要和borderWidth属性一起使用 385 backgroundColor: Color.White, 386 shadow: ({ 387 radius: 20, 388 color: Color.Grey, 389 offsetX: 50, 390 offsetY: 0 391 }), 392 confirm: { 393 defaultFocus: true, 394 value: 'Confirm button', 395 action: () => { 396 console.log('Get Alert Dialog handled'); 397 } 398 }, 399 cancel: () => { 400 console.log('actionSheet canceled'); 401 }, 402 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 403 console.info("reason=" + JSON.stringify(dismissDialogAction.reason)); 404 console.log("dialog onWillDismiss"); 405 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 406 dismissDialogAction.dismiss(); 407 } 408 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 409 dismissDialogAction.dismiss(); 410 } 411 }, 412 alignment: DialogAlignment.Bottom, 413 offset: { dx: 0, dy: -10 }, 414 sheets: [ 415 { 416 title: 'apples', 417 action: () => { 418 console.log('apples'); 419 } 420 }, 421 { 422 title: 'bananas', 423 action: () => { 424 console.log('bananas'); 425 } 426 }, 427 { 428 title: 'pears', 429 action: () => { 430 console.log('pears'); 431 } 432 } 433 ] 434 }) 435 }) 436 }.width('100%') 437 } 438} 439``` 440 441 442 443### 示例5(悬停态弹窗) 444 445该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。 446 447```ts 448// xxx.ets 449@Entry 450@Component 451struct ActionSheetExample { 452 build() { 453 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 454 Button('Click to Show ActionSheet') 455 .onClick(() => { 456 this.getUIContext().showActionSheet({ 457 title: 'ActionSheet title', 458 subtitle: 'ActionSheet subtitle', 459 message: 'message', 460 autoCancel: true, 461 confirm: { 462 defaultFocus: true, 463 value: 'Confirm button', 464 action: () => { 465 console.log('Get Alert Dialog handled'); 466 } 467 }, 468 cancel: () => { 469 console.log('actionSheet canceled'); 470 }, 471 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 472 console.info("reason=" + JSON.stringify(dismissDialogAction.reason)) 473 console.log("dialog onWillDismiss") 474 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 475 dismissDialogAction.dismiss(); 476 } 477 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 478 dismissDialogAction.dismiss(); 479 } 480 }, 481 alignment: DialogAlignment.Bottom, 482 offset: { dx: 0, dy: -10 }, 483 enableHoverMode: true, 484 hoverModeArea: HoverModeAreaType.TOP_SCREEN, 485 sheets: [ 486 { 487 title: 'apples', 488 action: () => { 489 console.log('apples'); 490 } 491 }, 492 { 493 title: 'bananas', 494 action: () => { 495 console.log('bananas'); 496 } 497 }, 498 { 499 title: 'pears', 500 action: () => { 501 console.log('pears'); 502 } 503 } 504 ] 505 }) 506 }) 507 }.width('100%') 508 .height('100%') 509 } 510} 511``` 512 513 514