HarmonyOS 鸿蒙Next点击问题

HarmonyOS 鸿蒙Next点击问题 Image正常显示的图片A,我在点击的时候想让Image显示图片B,点击结束又显示图片A(模拟button的点击效果),这个具体怎么实现呢

3 回复

可以参考此demo:

@Entry
@Component
struct ImageExample3 {
  private imageOne: Resource = $r('app.media.1');
  private imageTwo: Resource = $r('app.media.2');
  @State src: Resource = this.imageOne
  @State eventType: string = '事件'
  build(){
    Column(){
      Text(this.eventType)
        .margin(20)

      Image(this.src)
        .width(100)
        .height(100)
        .onTouch((event?: TouchEvent) => {
          if(event){
            if (event.type === TouchType.Down) {
              this.eventType = 'Down'
              this.src = this.imageTwo
            }
            if (event.type === TouchType.Up) {
              this.eventType = 'Up'
              this.src = this.imageOne
            }
          }
        })
    }.width('100%').height('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next点击问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


可以试一下图片加载使用背景图片加载,然后用多态样式设置按下的图片

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-polymorphic-style-V5

在HarmonyOS(鸿蒙)Next中,点击问题可能涉及事件分发机制或UI组件的触摸事件处理。鸿蒙系统采用分布式架构,事件处理与Android不同。鸿蒙的事件分发通过ComponentAbility完成,点击事件由ComponentonClick方法处理。如果点击无效,可能是事件未正确绑定或组件未启用点击功能。检查ComponentsetClickable属性是否为true,并确保事件监听器正确注册。此外,鸿蒙的UI框架使用ohos.agp包,点击事件可能被其他组件拦截或未正确处理。可通过日志排查事件分发流程,确认事件是否传递到目标组件。

回到顶部