在 OpenHarmony(ArkUI) 中,“菜单弹出”通常有以下几种常见方式,取决于你用的是 ArkTS(声明式开发) 还是 类Web/JS开发。下面以 ArkTS(主流方式) 为主说明。
Menu + bindMenu(最常用)适用于:点击控件弹出菜单
@Entry
@Component
struct MenuExample {
build() {
Column() {
Button('点击弹出菜单')
.bindMenu([
{
value: '复制',
action: () => {
console.info('点击了复制')
}
},
{
value: '粘贴',
action: () => {
console.info('点击了粘贴')
}
},
{
value: '删除',
action: () => {
console.info('点击了删除')
}
}
])
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
✅ 特点:
Menu + bindContextMenu(长按菜单)适用于:长按弹出菜单
Text('长按我')
.bindContextMenu([
{ value: '分享', action: () => {} },
{ value: '收藏', action: () => {} }
], ResponseType.LongPress)
CustomDialog 模拟菜单(自定义样式)适用于:需要复杂布局 / 自定义动画
@CustomDialog
struct MyMenuDialog {
controller: CustomDialogController
build() {
Column() {
Text('菜单项1').fontSize(18).padding(10)
Text('菜单项2').fontSize(18).padding(10)
Text('取消')
.padding(10)
.onClick(() => this.controller.close())
}
.width('60%')
.backgroundColor(Color.White)
.borderRadius(10)
}
}
@Entry
@Component
struct Page {
dialogController = new CustomDialogController({
builder: MyMenuDialog(),
alignment: DialogAlignment.Center
})
build() {
Button('显示菜单')
.onClick(() => {
this.dialogController.open()
})
}
}
Popup 弹出(轻量提示菜单)@State showMenu: boolean = false
Button('更多')
.onClick(() => this.showMenu = !this.showMenu)
.bindPopup(this.showMenu, {
message: '菜单内容',
onStateChange: (v) => this.showMenu = v.isVisible
})
| 场景 | 推荐方式 |
|---|---|
| 简单菜单 | bindMenu |
| 长按菜单 | bindContextMenu |
| 复杂 UI | CustomDialog |
| 轻量提示 | Popup |
如果你能说明:
我可以直接给你对应完整示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。