HarmonyOS CustomDialog 中cancel回调没有执行

点击步骤是:父组件点击–>CustomDialog open–>点击 蒙层–>cancel

HarmonyOS
2024-08-10 12:21:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

成功demo如下:

@CustomDialog 
struct CustomDialogExampleTwo { 
  controllerTwo?: CustomDialogController 
 
  build() { 
    Column() { 
      Text('我是第二个弹窗') 
        .fontSize(30) 
        .height(100) 
      Button('点我关闭第二个弹窗') 
        .onClick(() => { 
          if (this.controllerTwo != undefined) { 
            this.controllerTwo.close() 
          } 
        }) 
        .margin(20) 
    } 
  } 
} 
@CustomDialog 
struct CustomDialogExample_1 { 
  @Link textValue: string 
  @Link inputValue: string 
  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({ 
    builder: CustomDialogExampleTwo(), 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -25 } 
  }) 
  controller?: CustomDialogController 
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面 
  cancel: () => void = () => { 
  } 
  confirm: () => void = () => { 
  } 
  build() { 
    Column() { 
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) 
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%') 
        .onChange((value: string) => { 
          this.textValue = value 
        }) 
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 }) 
      Flex({ justifyContent: FlexAlign.SpaceAround }) { 
        Button('cancel') 
          .onClick(() => { 
            if (this.controller != undefined) { 
              this.controller.close() 
              this.cancel() 
            }}).backgroundColor(0xffffff).fontColor(Color.Black) 
        Button('confirm') 
          .onClick(() => { 
            if (this.controller != undefined) { 
              this.inputValue = this.textValue 
              this.controller.close() 
              this.confirm() 
            }}).backgroundColor(0xffffff).fontColor(Color.Red) 
      }.margin({ bottom: 10 }) 
      Button('点我打开第二个弹窗') 
        .onClick(() => { 
          if (this.dialogControllerTwo != null) { 
            this.dialogControllerTwo.open() 
          } 
        }) 
        .margin(20) 
    }.borderRadius(10) 
    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。 
  } 
} 
 
@Entry 
@Component 
struct CustomDialogUser_1 { 
  @State num :number = 0; 
  @State textValue: string = '' 
  @State inputValue: string = 'click me' 
  onCancel: () => void = () => { 
    this.num = this.num + 1; 
    this.textValue = '点击成功' + this.num + '次' 
    console.info('onCancle 执行') 
  }; 
  dialogController: CustomDialogController | null = new CustomDialogController({ 
    builder: CustomDialogExample_1({ 
      cancel: () => { 
        this.onCancel() 
      }, 
      confirm: () => { 
        this.onAccept() 
      }, 
      textValue: $textValue, 
      inputValue: $inputValue 
    }), 
    cancel: this.onCancel, // this.exitApp, 
    autoCancel: true, 
    alignment: DialogAlignment.Bottom, 
    offset: { dx: 0, dy: -20 }, 
    gridCount: 4, 
    customStyle: false, 
    cornerRadius: 10, 
  }) 
  // 在自定义组件即将析构销毁时将dialogControlle置空 
  aboutToDisappear() { 
    this.dialogController = null // 将dialogController置空 
  } 
  onAccept() { 
    console.info('Callback when the second button is clicked') 
  } 
  exitApp() { 
    console.info('Click the callback in the blank area') 
  } 
  build() { 
    Column() { 
      Button(this.inputValue) 
        .onClick(() => { 
          if (this.dialogController != null) { 
            this.dialogController.open() 
          } 
        }).backgroundColor(0x317aff) 
      Text(this.textValue) 
    }.width('100%').margin({ top: 5 }) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-10 18:21:09
相关问题
支付成功后没有收到
1504浏览 • 1回复 待解决
NAPI执行上层时,如何获取env
1844浏览 • 1回复 待解决
使用华为支付,没有支付成功的
120浏览 • 1回复 待解决
HarmonyOS onScrollyoffset不准确问题
123浏览 • 1回复 待解决
HarmonyOS 组件是否有销毁方法
98浏览 • 1回复 待解决
interface如何调用
637浏览 • 1回复 待解决