The menu bound to a component through bindContextMenu on a page can be closed as needed.
The initial APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
This example demonstrates how to call the ContextMenu.close API to close a context menu that is bound to a component using bindContextMenu.
You are advised to use the getContextMenuController API in UIContext to specify the UI execution context.
// 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(()=>{
// Close the menu when the component is dragged.
ContextMenu.close() // You are advised to use this.getUIContext().getContextMenuController().close() to obtain the UI context.
})
}
.width('100%')
.height('100%')
}
} 