@Entry
@Component
struct Page240522215803016 {
  @State message: string = 'Hello World';
  @State isToggleOn: boolean = false
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({
      confirm: () => {
        this.onAccept()
      },
    }),
  })

  onAccept() {
    if (this.isToggleOn) {
      this.isToggleOn = false
    } else {
      this.isToggleOn = true
    }
  }

  build() {
    RelativeContainer() {
      Toggle({
        type: ToggleType.Switch,
        isOn: this.isToggleOn
      })// 调用onTouchIntercept修改该组件的HitTestMode属性
        .onTouchIntercept((event: TouchEvent) => {
          console.log("OnTouchIntercept + " + JSON.stringify(event));
          this.dialogController.open()
          return HitTestMode.None
        }).onChange((isOn: boolean) => {
        console.info("isOn:" + isOn)
        if (isOn) {
          // 需要执行的操作
        }
      })
    }.height('100%').width('100%')
  }
}

@CustomDialog
export struct CustomDialogExample {
  controller: CustomDialogController = new CustomDialogController({ builder: CustomDialogExample({}), })
  confirm?: () => void

  build() {
    Column() {
      Text('点击确认').fontSize(20).margin({ top: 10, bottom: 10 }).onClick(() => {
        this.controller.close()
        if (this.confirm) {
          this.confirm()
        }
      })
    }
  }
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐