ts-methods-menu.md 1.6 KB
Newer Older
E
esterzhou 已提交
1 2
# Menu

E
ester.zhou 已提交
3
The menu bound to a component through [bindContextMenu](./ts-universal-attributes-menu.md#attributes) on a page can be closed as needed.
E
ester.zhou 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
esterzhou 已提交
8

E
ester.zhou 已提交
9

E
esterzhou 已提交
10
## ContextMenu.close
E
ester.zhou 已提交
11

E
ester.zhou 已提交
12 13
|Name|Description|
|----|---|
E
ester.zhou 已提交
14
| close(): void | Closes the menu bound to this component through [bindContextMenu](./ts-universal-attributes-menu.md#attributes) on a page.|
E
esterzhou 已提交
15

E
ester.zhou 已提交
16 17 18 19

## Example

```ts
E
ester.zhou 已提交
20 21
// xxx.ets
@Entry
E
ester.zhou 已提交
22 23 24 25
@Component
struct Index {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
E
ester.zhou 已提交
26
      Button('Test ContextMenu1')
E
ester.zhou 已提交
27
      Divider().strokeWidth(2).margin(5).color(Color.Black)
E
ester.zhou 已提交
28
      Button('Test ContextMenu2')
E
ester.zhou 已提交
29
      Divider().strokeWidth(2).margin(5).color(Color.Black)
E
ester.zhou 已提交
30 31 32 33
      Button('Test ContextMenu3')
    }
    .width(200)
    .height(160)
E
ester.zhou 已提交
34 35 36
  }

  build() {
E
ester.zhou 已提交
37
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
E
ester.zhou 已提交
38
      Column() {
E
ester.zhou 已提交
39 40 41 42 43 44 45 46 47 48 49 50
        Text("Test 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()
      })
E
esterzhou 已提交
51
    }
E
ester.zhou 已提交
52 53
    .width('100%')
    .height('100%')
E
esterzhou 已提交
54
  }
E
ester.zhou 已提交
55
}
E
ester.zhou 已提交
56
```
E
ester.zhou 已提交
57 58

![contextmenu_close.gif](figures/contextmenu_close.gif)