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不显示问题
100浏览 • 1回复 待解决
HarmonyOS 如果显示类似Toast提示
566浏览 • 2回复 待解决
HarmonyOS CustomDialogController 不显示
676浏览 • 1回复 待解决
HarmonyOS 应用退到后台显示toast方案
946浏览 • 1回复 待解决
HarmonyOS RelativeContainer内控件不显示
169浏览 • 1回复 待解决
HarmonyOS 应用图标不显示
360浏览 • 1回复 待解决
HarmonyOS webloadData不显示内容
195浏览 • 1回复 待解决
HarmonyOS web组件alert不显示
1171浏览 • 1回复 待解决
HarmonyOS 应用使用iconfont不显示
240浏览 • 1回复 待解决
HarmonyOS push通知不显示图片
145浏览 • 1回复 待解决
HarmonyOS Web组件不显示图片
128浏览 • 1回复 待解决
HarmonyOS listitem数据显示效果不一致
180浏览 • 1回复 待解决
Listitem点击变色有什么好方法吗
2497浏览 • 1回复 待解决