HarmonyOS menu如何用代码控制隐藏

当点击menu中的某一项时如何隐藏整个menu

HarmonyOS
2024-12-20 16:05:03
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

通过bindContextMenu,给组件绑定菜单,触发方式为控制绑定的isShown (文档示例7)参考地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-menu-V5#bindcontextmenu8

样例代码参考:

class classComponent {
  content: string = '';
  labelInfo: string = '';
}

@Entry
@Component
struct TestMenuPage {
  @State list1: Array<classComponent> = [{ content: '复制', labelInfo: "Ctrl+C" }, {
    content: "粘贴",
    labelInfo: "Ctrl+V"
  }];
  @State menuIsShow: boolean = false;
  @State menuBackGroundColor: Color = Color.White;
  @State menuIsDelete: boolean = false;

  @Builder
  MyMenu() {
    Menu() {
      ForEach(this.list1, (item: classComponent) => {
        MenuItem({ content: `${item.content}`, labelInfo: `${item.labelInfo}` })
      })
    }.visibility(this.menuIsDelete ? Visibility.None : Visibility.Visible)
  }

  build() {
    Column() {
      Column() {
        Button('点击').onClick(() => {
          this.menuIsShow = !this.menuIsShow;
        })
        Button('删除菜单').onClick(() => {
          this.list1 = []
          this.menuIsDelete = true
        })
      }
      .width('100%')
      .padding(20)
      .bindMenu(this.MyMenu)
      .bindContextMenu(this.menuIsShow, this.MyMenu(), {
        backgroundColor: this.menuIsDelete ? Color.Transparent : Color.White,
        backgroundColor: Color.White,
        backgroundBlurStyle: BlurStyle.NONE
      })
      .alignItems(HorizontalAlign.Start)
    }.height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
分享
微博
QQ
微信
回复
2024-12-20 18:45:15


相关问题
有人知道JS menu如何隐藏吗?
5017浏览 • 1回复 待解决
HarmonyOS TabContent控制显示隐藏
349浏览 • 1回复 待解决
HarmonyOS 如何代码控制软键盘弹出?
879浏览 • 1回复 待解决
HarmonyOS 弹出的Menu位置
483浏览 • 1回复 待解决