鸿蒙自定义对话框方法绑定

鸿蒙开发方法绑定注意事项,避免新手入坑
注意 Parent 结构体里面这两行代码
confirm1: this.onAccept,
confirm: (message:string,id:string): void => this.onAccept(message,id)

这两行代码实际意义不同

confirm1 这种方法绑定,是不能实现调用当前类的方法或者属性

confirm 使用箭头函数这种方式可以调用当前结构的属性或者方法,使用这种是正常情况

confirm: (message:string,id:string): void => this.onAccept(message,id),注意onAccept里面参数必须填写

@Component
export struct ListDemo{
  @Link arrtargetData : Array<TaskItemBean>;
  build(){
    Column(){
      ForEach(this.arrtargetData,
        (item: TaskItemBean) => {
          Button(`${item.taskName}`)
            .margin(12)
            .width(312)
            .height(40)
            .backgroundColor('#11a2a2a2')
            .fontColor('#e6000000')
        },
        (item: TaskItemBean) => item.taskName.toString()
      )
    }
  }
}
// xxx.ets
@CustomDialog
export struct CustomDialogDemo1 {
  @Link arrtargetData : Array<TaskItemBean>;
  @State valueText:string=''
  @State valueIDText:string=''
  controller: CustomDialogController
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在最后
  cancel: () => void
  confirm: (message:string,id:string) => void

  build() {
    Column() {
      Text('input task Name').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput().fontSize(20).margin({ top: 10, bottom: 10 }).onChange((value:string)=>{
        this.valueText=value
      })
      Text('input task ID').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput().fontSize(20).margin({ top: 10, bottom: 10 }).onChange((value:string)=>{
        this.valueIDText=(value)
      })

      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            this.confirm(this.valueText,this.valueIDText)
            this.controller.close()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
    // dialog默认的borderRadius为24vp,如果需要使用border属性,请和borderRadius属性一起使用。
  }
}

@Entry
@Component
struct Parent {
  @State arrtargetData : Array<TaskItemBean>=[new TaskItemBean('a', 1, 'a'),new TaskItemBean('b', 2, 'b')];
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogDemo1({
      cancel: this.onCancel,
//      confirm1: this.onAccept,
      confirm: (message:string,id:string): void => this.onAccept(message,id),
      arrtargetData:$arrtargetData
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false
  })
  existApp():void{

  }

  onAccept(message:string,id:string):void{
    this.arrtargetData.push(new TaskItemBean(message, Number(id), message))
  }

  onCancel():void{

  }
  build() {
    Column() {
     Button(`创建子任务`)
        .margin(12)
        .width(312)
        .height(40)
        .fontColor('#FFFFFF,90%')
        .onClick(() => {
          this.dialogController.open()
        })


      ListDemo({arrtargetData:$arrtargetData})


    }
  }
}

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值