在HarmonyOS NEXT开发中页面上创建两个button,只有一个可以收到click事件?

在HarmonyOS NEXT开发中页面上创建两个button,只有一个可以收到click事件?

@Entry 
@Component 
struct SplashScreenPage { 
  @State pageShowTime: number = CommonConstants.TIME_DEFAULT_VALUE; 
  @State intervalID: number = CommonConstants.INTERVAL_ID_DEFAULT; 
 
  build() { 
    Column() { 
      Stack({ alignContent: Alignment.TopStart }) { 
        Image($r('app.media.ic_splash_page_background')) 
          .width(CommonConstants.IMAGE_WIDTH) 
          .height(CommonConstants.IMAGE_HEIGHT) 
 
        HideButton(); 
        SkipButton({ secondsCount: (CommonConstants.DELAY_SECONDS - this.pageShowTime) }); 
      } 
      .layoutWeight(CommonConstants.STACK_LAYOUT_WEIGHT) 
      .width(CommonConstants.STACK_WIDTH); 
    } 
    .alignItems(HorizontalAlign.Start) 
    .width(CommonConstants.COLUMN_WIDTH) 
    .height(CommonConstants.COLUMN_HEIGHT) 
  } 
} 
 
//其次分别创建两个button 
 
@Component 
struct SkipButton { 
  @Prop secondsCount: number = 0; 
 
  build() { 
    Flex({ 
      direction: FlexDirection.Row, 
      justifyContent: FlexAlign.End 
    }) { 
      Text($r('app.string.skip', this.secondsCount)) 
        .onClick(() => { 
          console.log("EntryAbility Skip Button is clicked"); 
        }) 
    } 
  } 
} 
 
@Component 
struct HideButton { 
  build() { 
    Flex({ 
      direction: FlexDirection.Row, 
      justifyContent: FlexAlign.Start 
    }) { 
      Text($r('app.string.hide')) 
        .onClick(() => { 
          console.log("EntryAbility Hide Button is clicked"); 
        }) 
    } 
  } 
}
阅读 830
1 个回答

可以加入属性 .hitTestBehavior(HitTestMode.Transparent),参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...

@Component 
struct SkipButton { 
  @Prop secondsCount: number = 0; 
 
  build() { 
    Flex({ 
      direction: FlexDirection.Row, 
      justifyContent: FlexAlign.End 
    }) { 
      Text($r('app.string.app_name', this.secondsCount)) 
        .onClick(() => { 
          console.log("EntryAbility Skip Button is clicked"); 
        }) 
    } 
    .hitTestBehavior(HitTestMode.Transparent) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进