harmony 鸿蒙ContextMenu

  • 2025-06-12
  • 浏览 (4)

ContextMenu

在页面范围内关闭通过bindContextMenu属性绑定的菜单。

说明:

从API version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

ContextMenu.close(deprecated)

static close()

可以通过该方法在页面范围内关闭通过bindContextMenu给组件绑定的菜单。

说明:

从API version 18开始废弃,建议使用UIContext中的getContextMenuController获取ContextMenuController实例,再通过此实例调用替代方法close

从API version 12开始,可以通过使用UIContext中的getContextMenuController来明确UI的执行上下文。

原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

示例

该示例为ContextMenu.close关闭通过bindContextMenu属性绑定的菜单。

说明:

推荐通过使用UIContext中的getContextMenuController来明确UI的执行上下文。

// xxx.ets
@Entry
@Component
struct Index {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('Test ContextMenu1')
      Divider().strokeWidth(2).margin(5).color(Color.Black)
      Button('Test ContextMenu2')
      Divider().strokeWidth(2).margin(5).color(Color.Black)
      Button('Test ContextMenu3')
    }
    .width(200)
    .height(160)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Column() {
        Text("Test 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%')
  }
}

contextmenu_close.gif

你可能感兴趣的鸿蒙文章

harmony 鸿蒙图像AI分析错误码

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙弹出框 (Dialog)

harmony 鸿蒙DialogV2

0  赞