HarmonyOS XComponent没有点击事件

需要在XComponent组件上点击后隐藏周边的功能按钮,用onClick注册事件在点击的时候没有事件返回。

HarmonyOS
2025-01-09 16:39:24
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

XComponent组件可以响应onClick事件,参考示例如下:

// xxx.ets
import { trustedAppService } from '@kit.DeviceSecurityKit'

@Entry
@Component
struct PreviewArea {
  private surfaceId: string = ''
  private xComponentContext: Record<string, () => void> = {}
  xComponentController: XComponentController = new XComponentController()
  @State isSHow: Visibility = Visibility.Visible

  build() {
    Row() {
      XComponent({
        id: 'xcomponent',
        type: XComponentType.SURFACE,
        controller: this.xComponentController
      })
        .onClick(() => {
          if (this.isSHow == Visibility.Visible) {
            this.isSHow = Visibility.Hidden
          } else {
            this.isSHow = Visibility.Visible
          }
        })
        .backgroundColor(Color.Brown)
        .onLoad(() => {
          this.xComponentController.setXComponentSurfaceSize({ surfaceWidth: 1920, surfaceHeight: 1080 })
          this.surfaceId = this.xComponentController.getXComponentSurfaceId()
          this.xComponentContext = this.xComponentController.getXComponentContext() as Record<string, () => void>
        })
        .width('640px')
        .height('480px')
      Text('HarmonyOS')
        .visibility(this.isSHow)
    }
    .backgroundColor(Color.Orange)
    .position({ x: 0, y: 48 })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:29:30
相关问题
HarmonyOS 点击事件过滤
504浏览 • 1回复 待解决
HarmonyOS RNApp XComponent下面没有节点
409浏览 • 1回复 待解决
HarmonyOS 卡片点击事件失效
642浏览 • 1回复 待解决
HarmonyOS 富文本点击事件
1323浏览 • 1回复 待解决
HarmonyOS ReactNavigation点击事件穿透
480浏览 • 1回复 待解决
HarmonyOS 如何hook点击事件
673浏览 • 1回复 待解决
HarmonyOS 点击事件监听问题
644浏览 • 1回复 待解决
HarmonyOS 点击事件判定问题
678浏览 • 1回复 待解决
HarmonyOS 动态UI点击事件处理
797浏览 • 1回复 待解决
HarmonyOS 屏幕点击事件监控代码
924浏览 • 1回复 待解决
HarmonyOS点击事件传递响应问题
523浏览 • 1回复 待解决
HarmonyOS ImageSpan点击事件无法触发
968浏览 • 1回复 待解决
HarmonyOS 控件点击事件无响应
557浏览 • 1回复 待解决
HarmonyOS 回车触发点击事件
548浏览 • 1回复 待解决
Slider拦截点击事件失败
684浏览 • 1回复 待解决
HarmonyOS 点击事件的回调方法
802浏览 • 1回复 待解决
HarmonyOS onVisibleAreaChange事件没有触发
635浏览 • 1回复 待解决