HarmonyOS ContextMenu中的item点击不显示toast?

import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column({ space: 20 }) {
      Text("点击")
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          // 点击显示toast
          this.showToast()
        })

      Text("长按")
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .bindContextMenu(this.menu(), ResponseType.LongPress)

    }
    .height('100%')
    .width('100%')
  }

  showToast() {
    promptAction.showToast({
      message: "toast"
    })
  }

  @Builder
  menu() {
    Menu() {
      MenuItem({ content: "toast" })
        .contentFontColor(Color.Red)
        .onClick(() => {
          // 期望显示toast,实际不显示
          this.showToast()
        })
    }
  }
}
阅读 479
1 个回答

menu子窗显示的,子窗不允许弹子窗toast,可以给toast绑定一下uiContext

如下:

import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column({ space: 20 }) {
      Text("点击")
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          // 点击显示toast
          this.showToast()
        })

      Text("长按")
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .bindContextMenu(this.menu(), ResponseType.LongPress)

    }
    .height('100%')
    .width('100%')
  }

  showToast() {
    promptAction.showToast({
      message: "toast"
    })
  }

  @Builder
  menu() {
    Menu() {
      MenuItem({ content: "toast" })
        .contentFontColor(Color.Red)
        .onClick(() => {
          // 期望显示toast,实际不显示:修改处
          this.getUIContext().getPromptAction().showToast({
            message: "toast"
          })
        })
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进