1# 列表选择弹窗 (ActionSheet) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @houguobiao--> 5<!--Designer: @houguobiao--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9列表弹窗。 10 11> **说明:** 12> 13> 从API version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> 本模块功能依赖UI的执行上下文,不可在[UI上下文不明确](../../../ui/arkts-global-interface.md)的地方使用,参见[UIContext](../arkts-apis-uicontext-uicontext.md)说明。 16 17## ActionSheetOptions对象说明 18 19列表选择弹窗的样式。 20 21**系统能力:** SystemCapability.ArkUI.ArkUI.Full 22 23| 名称 | 类型 | 只读 | 可选 | 说明 | 24| ---------- | -------------------------- | ------- | ----------------------------- | ----------------------------- | 25| title | string \| [Resource](ts-types.md#resource) | 否 | 否 | 弹窗标题。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 26| subtitle<sup>10+</sup> | [ResourceStr](ts-types.md#resourcestr) | 否 | 是 | 弹窗副标题。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 27| message | string \| [Resource](ts-types.md#resource) | 否 | 否 | 弹窗内容。<br/>文本超长时会触发滚动条。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 28| autoCancel | boolean | 否 | 是 | 点击遮障层时,是否关闭弹窗。<br>默认值:true<br>值为true时,点击遮障层关闭弹窗,值为false时,点击遮障层不关闭弹窗。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 29| confirm | [ActionSheetButtonOptions](#actionsheetbuttonoptions18对象说明) | 否 | 是 | 确认Button的使能状态、默认焦点、按钮风格、文本内容和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键。多重弹窗情况下,可自动获焦并连续响应。默认响应Enter键能力在defaultFocus为true时不生效。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 30| cancel | [VoidCallback](ts-types.md#voidcallback12) | 否 | 是 | 点击遮障层关闭dialog时的回调。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 31| alignment | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 是 | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Bottom <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。<br/>**说明:** <br/>若在UIExtension中设置showInSubWindow为true,弹窗将基于UIExtension的宿主窗口对齐。| 32| 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开始,该接口支持在原子化服务中使用。| 33| sheets | Array<[SheetInfo](#sheetinfo对象说明)> | 否 | 否 | 设置选项内容,每个选择项支持设置图片、文本和选中的回调。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 34| 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开始,该接口支持在原子化服务中使用。| 35| showInSubWindow<sup>11+</sup> | boolean | 否 | 是 | 某弹窗需要显示在主窗口之外时,是否在子窗口显示此弹窗。值为true表示在子窗口显示弹窗。<br/>默认值:false,弹窗显示在应用内,而非独立子窗口。<br/>**说明:** <br/>showInSubWindow为true的弹窗无法触发显示另一个showInSubWindow为true的弹窗。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 36| isModal<sup>11+</sup> | boolean | 否 | 是 | 弹窗是否为模态窗口,模态窗口有蒙层,非模态窗口无蒙层。值为false时,弹窗为非模态窗口,无蒙层。<br/>默认值:true,此时弹窗有蒙层。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 37| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 否 | 是 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>backgroundColor会与模糊属性backgroundBlurStyle叠加产生效果,如果不符合预期,可将backgroundBlurStyle设置为BlurStyle.NONE,即可取消模糊。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 38| 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开始,该接口支持在原子化服务中使用。 | 39| backgroundBlurStyleOptions<sup>19+</sup> | [BackgroundBlurStyleOptions](ts-universal-attributes-background.md#backgroundblurstyleoptions10对象说明) | 否 | 是 | 背景模糊效果。默认值请参考BackgroundBlurStyleOptions类型说明。<br />**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 | 40| backgroundEffect<sup>19+</sup> | [BackgroundEffectOptions](ts-universal-attributes-background.md#backgroundeffectoptions11) | 否 | 是 | 背景效果参数。默认值请参考BackgroundEffectOptions类型说明。<br />**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 | 41| 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开始,该接口支持在原子化服务中使用。 | 42| 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开始,该接口支持在原子化服务中使用。| 43| 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开始,该接口支持在原子化服务中使用。| 44| 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开始,该接口支持在原子化服务中使用。| 45| 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开始,该接口支持在原子化服务中使用。 | 46| width<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | 否 | 是 | 设置弹窗背板的宽度。<br />**说明:**<br>- 弹窗宽度默认最大值:400vp。<br />- 百分比参数方式:弹窗参考宽度为所在窗口的宽度,在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 47| height<sup>12+</sup> | [Dimension](ts-types.md#dimension10) | 否 | 是 | 设置弹窗背板的高度。<br />**说明:**<br />- 弹窗高度默认最大值:0.9 *(窗口高度 - 安全区域)。<br />- 百分比参数方式:弹窗参考高度为(窗口高度 - 安全区域),在此基础上调小或调大。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 48| 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开始,该接口支持在原子化服务中使用。| 49| 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开始,该接口支持在原子化服务中使用。| 50| enableHoverMode<sup>14+</sup> | boolean | 否 | 是 | 是否响应悬停态,值为true表示响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 51| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-universal-attributes-sheet-transition.md#hovermodeareatype14) | 否 | 是 | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 52| onWillAppear<sup>19+</sup> | Callback<void> | 否 | 是 | 弹窗显示动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear >> onDidAppear >> onWillDisappear >> onDidDisappear。<br />2.在onWillAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。 <br/>**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。| 53| onDidAppear<sup>19+</sup> | Callback<void> | 否 | 是 | 弹窗弹出时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear >> onDidAppear >> onWillDisappear >> onDidDisappear。<br />2.在onDidAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br />3.快速点击弹出,关闭弹窗时,onWillDisappear在onDidAppear前生效。<br/>4.弹窗入场动效未完成时彻底关闭弹窗,动效打断,onDidAppear不会触发。<br/>**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 | 54| onWillDisappear<sup>19+</sup> | Callback<void> | 否 | 是 | 弹窗退出动效前的事件回调。<br />**说明:**<br />正常时序依次为:onWillAppear >> onDidAppear >> onWillDisappear >> onDidDisappear。<br /> **原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。| 55| onDidDisappear<sup>19+</sup> | Callback<void> | 否 | 是 | 弹窗消失时的事件回调。<br />**说明:**<br />正常时序依次为:onWillAppear >> onDidAppear >> onWillDisappear >> onDidDisappear。<br/>**原子化服务API:** 从API version 19开始,该接口支持在原子化服务中使用。 | 56| levelMode<sup>15+</sup> | [LevelMode](#levelmode15) | 否 | 是 | 设置弹窗显示层级。<br />**说明:**<br />- 默认值:LevelMode.OVERLAY <br />- 仅当showInSubWindow属性设置为false时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 57| levelUniqueId<sup>15+</sup> | number | 否 | 是 | 设置页面级弹窗需要显示的层级下的[节点 uniqueId](../js-apis-arkui-frameNode.md#getuniqueid12)。<br/>取值范围:大于等于0的数字。<br />**说明:**<br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 58| immersiveMode<sup>15+</sup> | [ImmersiveMode](#immersivemode15) | 否 | 是 | 设置页面内弹窗蒙层效果。<br />**说明:**<br />- 默认值:ImmersiveMode.DEFAULT <br />- 当且仅当levelMode属性设置为LevelMode.EMBEDDED时生效。<br/>**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。| 59| levelOrder<sup>18+</sup> | [LevelOrder](../js-apis-promptAction.md#levelorder18) | 否 | 是 | 设置弹窗显示的顺序。<br />**说明:**<br />- 默认值:LevelOrder.clamp(0) <br />- 不支持动态刷新顺序。<br/>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。| 60 61## SheetInfo对象说明 62 63弹窗中的选项内容,每一项支持设置文本、图标以及选中的回调。 64 65**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 66 67**系统能力:** SystemCapability.ArkUI.ArkUI.Full 68 69| 名称 | 类型 | 只读 | 可选 | 说明 | 70| ------ | ------------------------------------------------------------ | ---- | ----------------- | ----------------- | 71| title | string \| [Resource](ts-types.md#resource) | 否 | 否 | 选项的文本内容。<br/>文本超长时会触发滚动条。| 72| icon | string \| [Resource](ts-types.md#resource) | 否 | 是 | 选项的图标,默认无图标显示。<br>string格式可用于加载网络图片和本地图片,常用于加载网络图片。当使用相对路径引用本地图片时,例如Image("common/test.jpg")。| 73| action | [VoidCallback](ts-types.md#voidcallback12) | 否 | 否 | 选项选中的回调。 | 74 75## LevelMode<sup>15+</sup> 76 77type LevelMode = LevelMode 78 79弹窗的显示层级。 80 81**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 82 83**系统能力:** SystemCapability.ArkUI.ArkUI.Full 84 85| 类型 | 说明 | 86| ----------------------------------------------------------- | -------------------- | 87| [LevelMode](../js-apis-promptAction.md#levelmode15枚举说明) | 设置弹窗的显示层级。 | 88 89## ImmersiveMode<sup>15+</sup> 90 91type ImmersiveMode = ImmersiveMode 92 93弹窗的蒙层效果。 94 95**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 96 97**系统能力:** SystemCapability.ArkUI.ArkUI.Full 98 99| 类型 | 说明 | 100| ------------------------------------------------------------ | -------------------------- | 101| [ImmersiveMode](../js-apis-promptAction.md#immersivemode15枚举说明) | 设置页面内弹窗的蒙层效果。 | 102 103## DismissDialogAction<sup>12+</sup> 104 105Dialog关闭的信息。 106 107**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 108 109**系统能力:** SystemCapability.ArkUI.ArkUI.Full 110 111### 属性 112 113| 名称 | 类型 | 只读 | 可选 | 说明 | 114| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 115| dismiss | Callback<void> | 否 | 否 | Dialog关闭回调函数。开发者需要退出时调用,不需要退出时无需调用。 | 116| reason | [DismissReason](ts-universal-attributes-popup.md#dismissreason12枚举说明) | 否 | 否 | Dialog无法关闭原因。根据开发者需求选择不同操作下,Dialog是否关闭。 | 117 118## ActionSheetButtonOptions<sup>18+</sup>对象说明 119 120弹窗中按钮的样式。 121 122> **说明:** 123> 124> 为规范匿名对象的定义,API 18版本修改了此处的元素定义。其中,保留了历史匿名对象的起始版本信息,会出现外层元素@since版本号高于内层元素版本号的情况,但这不影响接口的使用。 125 126**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 127 128**系统能力:** SystemCapability.ArkUI.ArkUI.Full 129 130| 名称 | 类型 | 只读 | 可选 | 说明 | 131| ------------ | ------- | ---- | ---- | ---- | 132| enabled<sup>10+</sup> | boolean | 否 | 是 | 点击Button是否响应,true表示Button可以响应,false表示Button不可以响应。<br/>默认值:true<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 133| defaultFocus<sup>10+</sup> | boolean | 否 | 是 | 设置Button是否是默认焦点,true表示Button是默认焦点,false表示Button不是默认焦点。<br/>默认值:false<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 134| style<sup>10+</sup> | [DialogButtonStyle](ts-appendix-enums.md#dialogbuttonstyle10) | 否 | 是 | 设置Button的风格样式。<br />默认值:DialogButtonStyle.DEFAULT<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 135| value<sup>8+</sup> | string \| [Resource](ts-types.md#resource) | 否 | 否 | Button文本内容。<br/>当文本内容过长无法显示时,用省略号代替未显示的部分。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 136| action<sup>8+</sup> | [VoidCallback](ts-types.md#voidcallback12) | 否 | 否 | Button选中时的回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 137 138## ActionSheetOffset<sup>18+</sup>对象说明 139 140弹窗的对齐方式。 141 142**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 143 144**系统能力:** SystemCapability.ArkUI.ArkUI.Full 145 146| 名称 | 类型 | 只读 | 可选 | 说明 | 147| ---- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 148| dx | number \| string \| [Resource](ts-types.md#resource) | 否 | 否 | 弹出窗口相对于对齐位置dx的偏移量。<br/>需要显式指定像素单位,如'10px',也可设置百分比字符串,如'100%'。<br/>**说明:** <br/>不指定像素单位时,默认单位vp,如'10',等同于10。 | 149| dy | number \| string \| [Resource](ts-types.md#resource) | 否 | 否 | 弹出窗口相对于对齐位置dy的偏移量。<br/>需要显式指定像素单位,如'10px',也可设置百分比字符串,如'100%'。<br/>**说明:** <br/>不指定像素单位时,默认单位vp,如'10',等同于10。 | 150 151## ActionSheet 152 153### show<sup>(deprecated)</sup> 154 155static show(value: ActionSheetOptions) 156 157定义列表弹窗并弹出。 158 159> **说明:** 160> 161> 从API version 18开始废弃,建议使用[UIContext](../arkts-apis-uicontext-uicontext.md)中的[showActionSheet](../arkts-apis-uicontext-uicontext.md#showactionsheet)替代。 162> 163> 从API version 10开始,可以通过使用[UIContext](../arkts-apis-uicontext-uicontext.md)中的[showActionSheet](../arkts-apis-uicontext-uicontext.md#showactionsheet)来明确UI的执行上下文。 164 165**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 166 167**系统能力:** SystemCapability.ArkUI.ArkUI.Full 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| ------ | ------------------------------------------------- | ---- | ------------------------ | 173| value | [ActionSheetOptions](#actionsheetoptions对象说明) | 是 | 配置列表选择弹窗的参数。 | 174 175## 示例 176 177> **说明:** 178> 179> 直接使用ActionSheet可能导致实例不明确的问题,建议使用getUIContext()获取[UIContext](../arkts-apis-uicontext-uicontext.md)实例,并使用[showActionSheet](../arkts-apis-uicontext-uicontext.md#showactionsheet)调用绑定实例的ActionSheet.show()。 180 181### 示例1(弹出列表选择弹窗) 182 183该示例通过点击按钮弹窗列表选择弹窗。 184 185```ts 186// xxx.ets 187@Entry 188@Component 189struct ActionSheetExample { 190 build() { 191 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 192 Button('Click to Show ActionSheet') 193 .onClick(() => { 194 this.getUIContext().showActionSheet({ 195 title: 'ActionSheet title', 196 subtitle: 'ActionSheet subtitle', 197 message: 'message', 198 autoCancel: true, 199 confirm: { 200 defaultFocus: true, 201 value: 'Confirm button', 202 action: () => { 203 console.info('Get ActionSheet handled'); 204 } 205 }, 206 cancel: () => { 207 console.info('ActionSheet canceled'); 208 }, 209 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 210 console.info(`reason= ${dismissDialogAction.reason}`); 211 console.info('ActionSheet onWillDismiss'); 212 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 213 dismissDialogAction.dismiss(); 214 } 215 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 216 dismissDialogAction.dismiss(); 217 } 218 }, 219 alignment: DialogAlignment.Bottom, 220 offset: { dx: 0, dy: -10 }, 221 sheets: [ 222 { 223 title: 'apples', 224 action: () => { 225 console.info('apples'); 226 } 227 }, 228 { 229 title: 'bananas', 230 action: () => { 231 console.info('bananas'); 232 } 233 }, 234 { 235 title: 'pears', 236 action: () => { 237 console.info('pears'); 238 } 239 } 240 ] 241 }) 242 }) 243 }.width('100%') 244 .height('100%') 245 } 246} 247``` 248 249 250 251### 示例2(可在主窗外弹出的弹窗) 252 253在2in1设备上设置showInSubWindow为true时,可以弹出在主窗外显示的弹窗。 254 255```ts 256// xxx.ets 257@Entry 258@Component 259struct ActionSheetExample { 260 build() { 261 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 262 Button('Click to Show ActionSheet') 263 .onClick(() => { 264 this.getUIContext().showActionSheet({ 265 title: 'ActionSheet title', 266 subtitle: 'ActionSheet subtitle', 267 message: 'message', 268 autoCancel: true, 269 showInSubWindow: true, 270 isModal: true, 271 confirm: { 272 defaultFocus: true, 273 value: 'Confirm button', 274 action: () => { 275 console.info('Get ActionSheet handled'); 276 } 277 }, 278 cancel: () => { 279 console.info('ActionSheet canceled'); 280 }, 281 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 282 console.info(`reason= ${dismissDialogAction.reason}`); 283 console.info('ActionSheet onWillDismiss'); 284 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 285 dismissDialogAction.dismiss(); 286 } 287 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 288 dismissDialogAction.dismiss(); 289 } 290 }, 291 alignment: DialogAlignment.Center, 292 offset: { dx: 0, dy: -10 }, 293 sheets: [ 294 { 295 title: 'apples', 296 action: () => { 297 console.info('apples'); 298 } 299 }, 300 { 301 title: 'bananas', 302 action: () => { 303 console.info('bananas'); 304 } 305 }, 306 { 307 title: 'pears', 308 action: () => { 309 console.info('pears'); 310 } 311 } 312 ] 313 }) 314 }) 315 }.width('100%') 316 .height('100%') 317 } 318} 319``` 320 321 322 323### 示例3(设置弹窗的动画) 324 325该示例通过配置transition实现弹窗的显示和消失动画。 326 327```ts 328// xxx.ets 329@Entry 330@Component 331struct ActionSheetExample { 332 build() { 333 Column({ space: 5 }) { 334 Button('ActionSheet Set Duration') 335 .onClick(() => { 336 this.getUIContext().showActionSheet({ 337 title: 'ActionSheet 1', 338 message: 'Set Animation Duration open 3 second, close 100 ms', 339 autoCancel: true, 340 alignment: DialogAlignment.Top, 341 transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY 342 .animation({ duration: 3000, curve: Curve.Sharp }) 343 .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 }).animation({ duration: 3000, curve: Curve.Sharp })), 344 TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) 345 .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 }).animation({ duration: 100, curve: Curve.Smooth }))), 346 offset: { dx: 0, dy: -20 }, 347 confirm: { 348 value: 'button', 349 action: () => { 350 console.info('Button-clicking callback'); 351 } 352 }, 353 cancel: () => { 354 console.info('Closed callbacks'); 355 }, 356 sheets: [ 357 { 358 title: 'apples', 359 action: () => { 360 console.info('apples'); 361 } 362 }, 363 { 364 title: 'bananas', 365 action: () => { 366 console.info('bananas'); 367 } 368 }, 369 { 370 title: 'pears', 371 action: () => { 372 console.info('pears'); 373 } 374 } 375 ] 376 }) 377 }).backgroundColor(0x317aff).height("88px") 378 }.width('100%').margin({ top: 5 }) 379 } 380} 381``` 382 383 384 385### 示例4(设置弹窗的样式) 386 387该示例定义了ActionSheet的样式,如宽度、高度、背景色、阴影等。 388 389```ts 390// xxx.ets 391@Entry 392@Component 393struct ActionSheetExample { 394 build() { 395 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 396 Button('Click to Show ActionSheet') 397 .onClick(() => { 398 this.getUIContext().showActionSheet({ 399 title: 'ActionSheet title', 400 subtitle: 'ActionSheet subtitle', 401 message: 'message', 402 autoCancel: true, 403 width: 300, 404 height: 350, 405 cornerRadius: 20, 406 borderWidth: 1, 407 borderStyle: BorderStyle.Solid, //使用borderStyle属性,需要和borderWidth属性一起使用 408 borderColor: Color.Blue, //使用borderColor属性,需要和borderWidth属性一起使用 409 backgroundColor: Color.White, 410 shadow: ({ 411 radius: 20, 412 color: Color.Grey, 413 offsetX: 50, 414 offsetY: 0 415 }), 416 confirm: { 417 defaultFocus: true, 418 value: 'Confirm button', 419 action: () => { 420 console.info('Get ActionSheet handled'); 421 } 422 }, 423 cancel: () => { 424 console.info('ActionSheet canceled'); 425 }, 426 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 427 console.info(`reason= ${dismissDialogAction.reason}`); 428 console.info('ActionSheet onWillDismiss'); 429 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 430 dismissDialogAction.dismiss(); 431 } 432 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 433 dismissDialogAction.dismiss(); 434 } 435 }, 436 alignment: DialogAlignment.Bottom, 437 offset: { dx: 0, dy: -10 }, 438 sheets: [ 439 { 440 title: 'apples', 441 action: () => { 442 console.info('apples'); 443 } 444 }, 445 { 446 title: 'bananas', 447 action: () => { 448 console.info('bananas'); 449 } 450 }, 451 { 452 title: 'pears', 453 action: () => { 454 console.info('pears'); 455 } 456 } 457 ] 458 }) 459 }) 460 }.width('100%') 461 } 462} 463``` 464 465 466 467### 示例5(悬停态弹窗) 468 469<!--RP1-->该示例展示了在悬停态下设置dialog布局区域的效果。<!--RP1End--> 470 471```ts 472// xxx.ets 473@Entry 474@Component 475struct ActionSheetExample { 476 build() { 477 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 478 Button('Click to Show ActionSheet') 479 .onClick(() => { 480 this.getUIContext().showActionSheet({ 481 title: 'ActionSheet title', 482 subtitle: 'ActionSheet subtitle', 483 message: 'message', 484 autoCancel: true, 485 confirm: { 486 defaultFocus: true, 487 value: 'Confirm button', 488 action: () => { 489 console.info('Get ActionSheet handled'); 490 } 491 }, 492 cancel: () => { 493 console.info('ActionSheet canceled'); 494 }, 495 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 496 console.info(`reason= ${dismissDialogAction.reason}`); 497 console.info('ActionSheet onWillDismiss'); 498 if (dismissDialogAction.reason === DismissReason.PRESS_BACK) { 499 dismissDialogAction.dismiss(); 500 } 501 if (dismissDialogAction.reason === DismissReason.TOUCH_OUTSIDE) { 502 dismissDialogAction.dismiss(); 503 } 504 }, 505 alignment: DialogAlignment.Bottom, 506 offset: { dx: 0, dy: -10 }, 507 enableHoverMode: true, 508 hoverModeArea: HoverModeAreaType.TOP_SCREEN, 509 sheets: [ 510 { 511 title: 'apples', 512 action: () => { 513 console.info('apples'); 514 } 515 }, 516 { 517 title: 'bananas', 518 action: () => { 519 console.info('bananas'); 520 } 521 }, 522 { 523 title: 'pears', 524 action: () => { 525 console.info('pears'); 526 } 527 } 528 ] 529 }) 530 }) 531 }.width('100%') 532 .height('100%') 533 } 534} 535``` 536 537<!--RP2--><!--RP2End--> 538 539### 示例6(弹窗生命周期) 540 541该示例展示了弹窗生命周期的相关接口的使用方法。 542 543```ts 544// xxx.ets 545@Entry 546@Component 547struct Example1 { 548 @State log: string = 'Log information:'; 549 flag: boolean = false; 550 551 build() { 552 Column({ space: 5 }) { 553 Button('ActionSheet') 554 .onClick(() => { 555 this.getUIContext().showActionSheet({ 556 title: 'ActionSheet', 557 message: 'message', 558 autoCancel: true, 559 alignment: DialogAlignment.Bottom, 560 offset: { dx: 0, dy: -20 }, 561 confirm: { 562 value: 'button', 563 action: () => { 564 console.info('ActionSheet Button-clicking callback'); 565 } 566 }, 567 cancel: () => { 568 console.info('ActionSheet Closed callbacks'); 569 }, 570 sheets: [ 571 { 572 title: 'apples', 573 action: () => { 574 console.info('ActionSheet apples') 575 } 576 }, 577 { 578 title: 'bananas', 579 action: () => { 580 console.info('ActionSheet bananas') 581 } 582 }, 583 { 584 title: 'pears', 585 action: () => { 586 console.info('ActionSheet pears') 587 } 588 } 589 ], 590 onDidAppear: () => { 591 this.log += '# onDidAppear'; 592 console.info('ActionSheet,is onDidAppear!'); 593 }, 594 onDidDisappear: () => { 595 this.log += '# onDidDisappear'; 596 console.info('ActionSheet,is onDidDisappear!'); 597 }, 598 onWillAppear: () => { 599 this.log = 'Log information:onWillAppear'; 600 console.info('ActionSheet,is onWillAppear!'); 601 }, 602 onWillDisappear: () => { 603 this.log += '# onWillDisappear'; 604 console.info('ActionSheet,is onWillDisappear!'); 605 } 606 }) 607 }) 608 Text(this.log).fontSize(30).margin({ top: 200 }) 609 }.width('100%').margin({ top: 5 }) 610 } 611} 612``` 613 614 615 616### 示例7(自定义背景模糊效果参数) 617 618从API version 19开始,该示例通过配置[backgroundBlurStyleOptions](#actionsheetoptions对象说明),实现自定义背景模糊效果。 619 620```ts 621@Entry 622@Component 623struct ActionSheetExample { 624 build() { 625 Stack({ alignContent: Alignment.Top }) { 626 Image($r('app.media.bg')) 627 Column() { 628 Button("ActionSheet") 629 .margin(20) 630 .onClick(() => { 631 this.getUIContext().showActionSheet({ 632 title: 'ActionSheet Title', 633 subtitle: 'ActionSheet Subtitle', 634 message: 'ActionSheet Text', 635 sheets: [ 636 { 637 title: 'Apples', 638 action: () => { 639 console.info('apples'); 640 } 641 }, 642 { 643 title: 'Bananas', 644 action: () => { 645 console.info('bananas'); 646 } 647 }, 648 { 649 title: 'Pears', 650 action: () => { 651 console.info('pears'); 652 } 653 } 654 ], 655 alignment: DialogAlignment.Center, 656 backgroundColor: undefined, 657 backgroundBlurStyle: BlurStyle.Thin, 658 backgroundBlurStyleOptions: { 659 colorMode: ThemeColorMode.LIGHT, 660 adaptiveColor: AdaptiveColor.AVERAGE, 661 scale: 1, 662 blurOptions: { grayscale: [20, 20] }, 663 }, 664 }); 665 }) 666 }.width('100%') 667 } 668 } 669} 670``` 671 672 673 674### 示例8(自定义背景效果参数) 675 676从API version 19开始,该示例通过配置[backgroundEffect](#actionsheetoptions对象说明),实现自定义背景效果。 677 678```ts 679@Entry 680@Component 681struct ActionSheetExample { 682 build() { 683 Stack({ alignContent: Alignment.Top }) { 684 Image($r('app.media.bg')) 685 Column() { 686 Button("ActionSheet") 687 .margin(20) 688 .onClick(() => { 689 this.getUIContext().showActionSheet({ 690 title: 'ActionSheet Title', 691 subtitle: 'ActionSheet Subtitle', 692 message: 'ActionSheet Text', 693 sheets: [ 694 { 695 title: 'Apples', 696 action: () => { 697 console.info('apples'); 698 } 699 }, 700 { 701 title: 'Bananas', 702 action: () => { 703 console.info('bananas'); 704 } 705 }, 706 { 707 title: 'Pears', 708 action: () => { 709 console.info('pears'); 710 } 711 } 712 ], 713 alignment: DialogAlignment.Center, 714 backgroundColor: undefined, 715 backgroundBlurStyle: BlurStyle.Thin, 716 backgroundEffect: { 717 radius: 60, 718 saturation: 0, 719 brightness: 1, 720 color: Color.White, 721 blurOptions: { grayscale: [20, 20] } 722 }, 723 }); 724 }) 725 }.width('100%') 726 } 727 } 728} 729``` 730