在页面范围内关闭通过bindContextMenu属性绑定的菜单。
从API version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
该示例演示ContextMenu.close的使用方法,在拖拽开始时关闭通过bindContextMenu绑定的菜单。
// xxx.ets
@Entry
@Component
struct Index {
@Builder MenuBuilder() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button('ContextMenu1')
Divider().strokeWidth(2).margin(5).color(Color.Black)
Button('ContextMenu2')
Divider().strokeWidth(2).margin(5).color(Color.Black)
Button('ContextMenu3')
}
.width(200)
.height(160)
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Column() {
Text('Long press to show ContextMenu')
.fontSize(20)
.width('100%')
.height(500)
.backgroundColor(0xAFEEEE)
.textAlign(TextAlign.Center)
}
.bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
.onDragStart(() => {
// 拖拽时关闭菜单
ContextMenu.close() // 建议使用 this.getUIContext().getContextMenuController().close()
})
}
.width('100%')
.height('100%')
}
} 