ts-universal-attributes-menu.md 3.9 KB
Newer Older
E
esterzhou 已提交
1
# Menu Control
Z
zengyawen 已提交
2

3 4 5 6 7
This module provides APIs for binding a menu – a vertical list of items – to a component. This menu is displayed by long-pressing, clicking, or right-clicking the component.

>  **NOTE**
>
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

E
esterzhou 已提交
9 10

## Required Permissions
Z
zengyawen 已提交
11 12 13

None

E
esterzhou 已提交
14 15 16 17

## Attributes


18 19 20 21
| Name                          | Type                                    | Default Value  | Description                                |
| ---------------------------- | ---------------------------------------- | ---- | ---------------------------------- |
| bindMenu                     | Array<MenuItem&gt;&nbsp;\|&nbsp;[CustomBuilder](../../ui/ts-types.md)<sup>8+</sup> | -    | Menu bound to the component, which is displayed when you click the component. Textual and custom menu items are supported.|
| bindContextMenu<sup>8+</sup> | content:&nbsp;[CustomBuilder](../../ui/ts-types.md),<br>responseType:&nbsp;ResponseType | -    | Context menu bound to the component, which is displayed when you long-press or right-click the component. Only custom menu items are supported. |
E
esterzhou 已提交
22 23 24


- MenuItem
25 26 27 28
  | Name    | Type                     | Description         |
  | ------ | ----------------------- | ----------- |
  | value  | string                  | Menu item text.     |
  | action | ()&nbsp;=&gt;&nbsp;void | Action triggered when a menu item is clicked.|
E
esterzhou 已提交
29 30

- ResponseType<sup>8+</sup>
31 32 33 34
  | Value       | Description           |
  | ---------- | ------------- |
  | LongPress  | The menu is displayed when the component is long-pressed.  |
  | RightClick | The menu is displayed when the component is right-clicked.|
E
esterzhou 已提交
35 36 37 38

## Example

#### Menu with Textual Menu Items
Z
zengyawen 已提交
39

40 41
```ts
// xxx.ets
Z
zengyawen 已提交
42 43
@Entry
@Component
Z
zengyawen 已提交
44
struct MenuExample {
Z
zengyawen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  build() {
    Column() {
      Text('click for Menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindMenu([
      {
        value: 'Menu1',
        action: () => {
          console.info('handle Menu1 select')
        }
      },
      {
        value: 'Menu2',
        action: () => {
          console.info('handle Menu2 select')
        }
      },
    ])
  }
}
```

E
esterzhou 已提交
69 70 71
![en_image_0000001174582862](figures/en_image_0000001174582862.gif)

#### Menu with Custom Menu Items
Z
zengyawen 已提交
72

73 74
```ts
// xxx.ets
Z
zengyawen 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
import router from '@system.router';

@Entry
@Component
struct MenuExample {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text('text1')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)

      Divider().height(10)

      Text('text2')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)

      Divider().height(10)

      Button('Next')
        .fontSize(20)
        .width(100)
        .height(50)
        .onClick(() => {
          router.push({ uri: 'pages/details' })
        })

    }.width(100)
  }

  build() {
    Column() {
      Text('click for menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindMenu(this.MenuBuilder)
  }
}
```

E
esterzhou 已提交
120 121 122
![en_image_0000001186807708](figures/en_image_0000001186807708.gif)

#### Context Menu (Displayed Upon Right-Clicking)
Z
zengyawen 已提交
123

124 125
```ts
// xxx.ets
E
esterzhou 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
@Entry
@Component
struct ContextMenuExample {
  @Builder MenuBuilder() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Text('Test menu item 1')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
      Divider().height(10)
      Text('Test menu item 2')
        .fontSize(20)
        .width(100)
        .height(50)
        .textAlign(TextAlign.Center)
    }.width(100)
  }
  
  build() {
    Column() {
      Text('rightclick for menu')
    }
    .width('100%')
    .margin({ top: 5 })
    .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
  }
}
```