HarmonyOS应用开发学习笔记 arkTS自定义弹窗(CustomDialog)简单使用 arkTS弹出框回调、监听

HarmonyOS应用开发学习笔记 arkTS自定义弹窗(CustomDialog)简单使用

1、@CustomDialog装饰器用于装饰自定义弹框

在这里插入图片描述

1、定义弹出框 @CustomDialog

@CustomDialog
export struct CustomDialogExample {
  controller: CustomDialogController

  build() {
    Column() {
      Text("是否退出?").fontSize(30).margin({ top: 60 })
      Blank()

      Row() {
        Text('是')
          .width('50%')
          .height(40)
          .backgroundColor(Color.Blue)
          .fontColor(Color.White)
          .fontSize(18)
          .textAlign(TextAlign.Center)
        Text('否')
          .width('50%')
          .height(40)
          .backgroundColor(Color.Gray)
          .fontColor(Color.White)
          .fontSize(18)
          .textAlign(TextAlign.Center)
      }.backgroundColor(Color.Red)
    }.width('100%').height(200)
  }
}

2、使用 .open()

  private dialog =
    new CustomDialogController({ builder: CustomDialogExample() })
	……

        Button("拥抱时代")
          .width('80%')
          .margin({ left: 20, top:200, right: 20 })
          .onClick(() => {
            this.dialog.open() //淡出淡出狂
          })

3、弹出框添加回调

在这里插入图片描述
在这里插入图片描述

  • 使用的地方代码
  private dialog = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: this.onCancel,
      confirm: this.onAccept
    })
  })

  //定义onCancel回调方法
  onCancel() {
    console.info('Callback when the first button is clicked')
  }
  //onAccept
  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  • 弹出框的代码
@CustomDialog
export struct CustomDialogExample {
  controller: CustomDialogController //弹出框控制器
  cancel: () => void //回调方法cancel
  confirm: () => void //回调方法confirm


  build() {
    Column() {
      Text("是否退出?").fontSize(30).margin({ top: 60 })
      Blank()

      Row() {
        Text('是')
          .width('50%').height(40).backgroundColor(Color.Blue).fontColor(Color.White)
          .fontSize(18).textAlign(TextAlign.Center)
          .onClick(() => {
            this.controller.close()
            this.confirm() //调用回调
          })

        Text('否')
          .width('50%').height(40).backgroundColor(Color.Gray).fontColor(Color.White)
          .fontSize(18).textAlign(TextAlign.Center)
          .onClick(() => {
            this.controller.close()
            this.cancel() //调用回调
          })


      }.backgroundColor(Color.Red)
    }.width('100%').height(200)
  }
}

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 21:04:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 21:04:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 21:04:02       87 阅读
  4. Python语言-面向对象

    2024-01-11 21:04:02       96 阅读

热门阅读

  1. 我国实施个人信息出境认证的要点

    2024-01-11 21:04:02       54 阅读
  2. SpringMVC-03

    2024-01-11 21:04:02       56 阅读
  3. Vue父子组件值的传递【极简版】

    2024-01-11 21:04:02       57 阅读
  4. CMake编译选项CMAKE_CXX_FLAGS详解

    2024-01-11 21:04:02       48 阅读
  5. Ubuntu查看内存使用情况

    2024-01-11 21:04:02       49 阅读
  6. 【PHP】价格区间字段验证,如4万-5万

    2024-01-11 21:04:02       46 阅读