Skip to content

菜单控制 Menu

Menu 是菜单接口,一般用于鼠标右键弹窗、点击弹窗等。

  1. 使用 bindMenubindContextMenu 未设置预览图时,菜单弹出无蒙层,此时在本页面

  2. 使用bindContextMenu并设置预览图,菜单弹出时有蒙层,此时不在本页面

默认样式

ts
@Entry
@Component
struct Index7 {
    build() {
        Column() {
            // 默认
            Row() {
                Button("默认")
                    .type(ButtonType.Normal)
                    .bindMenu([
                        {
                            value: "列表第一个",
                            action: () => {
                                console.info("我点击了第1个")
                            }
                        },
                        {
                            value: "列表第二个",
                            action: () => {
                                console.info("我点击了第2个")
                            }
                        }
                    ])
            }
        }
    }
}

自定义菜单

ts

@Entry
@Component
struct Index7 {
    @Builder
    slot() {
        Column({ space: 20 }) {
            Row() {
                Text("这就是内容").fontSize(24).fontColor(Color.Red)
            }

            Row({ space: 20 }) {
                Button("确定")
                    .type(ButtonType.Normal)
                    .onClick(() => {
                        console.info("点击了确定")
                    })
                Button("取消").type(ButtonType.Normal).onClick(() => {
                    console.info("点击了取消")
                })
            }
        }.padding({
            top: 15,
            bottom: 15
        })
    }

    build() {
        Column({ space: 20 }) {
            // 默认
            Row() {
                Button("默认")
                    .type(ButtonType.Normal)
                    .bindMenu([
                        {
                            value: "列表第一个",
                            action: () => {
                                console.info("我点击了第1个")
                            }
                        },
                        {
                            value: "列表第二个",
                            action: () => {
                                console.info("我点击了第2个")
                            }
                        }
                    ])
            }

            // 自定义菜单

            Row() {
                Button("点击触发")
                    .type(ButtonType.Normal)
                    .bindMenu(this.slot)
            }
        }
    }
}

长按或者鼠标右键(仅支持自定义)

  • 两个参数 ,一个是 slot 一个是类型
ts
Row() {
    Button("点击触发")
        .type(ButtonType.Normal)
        .bindContextMenu(this.slot, ResponseType.LongPress)
}

Row() {
    Button("点击触发")
        .type(ButtonType.Normal)
        .bindContextMenu(this.slot, ResponseType.RightClick)
}