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()
        })
    }
  }
}
HarmonyOS
2024-12-24 17:45:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

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"
          })
        })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-24 20:10:00
相关问题
HarmonyOS toast不显示问题
1001浏览 • 1回复 待解决
HarmonyOS 如果显示类似Toast提示
1097浏览 • 1回复 待解决
HarmonyOS CustomDialogController 不显示
1544浏览 • 1回复 待解决
HarmonyOS 应用退到后台显示toast方案
2031浏览 • 1回复 待解决
HarmonyOS RelativeContainer内控件不显示
787浏览 • 1回复 待解决
HarmonyOS webloadData不显示内容
814浏览 • 1回复 待解决
HarmonyOS 应用图标不显示
1334浏览 • 1回复 待解决
HarmonyOS push通知不显示图片
837浏览 • 1回复 待解决
HarmonyOS Web组件不显示图片
913浏览 • 1回复 待解决
HarmonyOS 应用使用iconfont不显示
986浏览 • 1回复 待解决
HarmonyOS web组件alert不显示
2212浏览 • 1回复 待解决
HarmonyOS web加载页面图片不显示
1967浏览 • 1回复 待解决