dsttl3.xyz

  • 隐私政策
  • dstt
dsttl3
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 未分类
  3. 正文

鸿蒙next自定义弹窗

1970-01-01 42点热度 0人点赞

自定义弹窗组件MyDialog

@CustomDialog
export struct MyDialog {
  title?: string
  cancelBtnText?: string
  confirmBtnText?: string
  message?: string
  cancel: () => void = () => {}
  confirm: () => void = () => {}
  controller: CustomDialogController

  build() {
    Column({space: 16 }) {
      Text(this.title).fontSize(20).fontWeight(FontWeight.Bold)
      Text(this.message){
      }.fontSize(16)
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button(this.cancelBtnText)
          .onClick(() => {
            this.cancel()
            this.controller.close()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button(this.confirmBtnText)
          .onClick(() => {
            this.confirm()
            this.controller.close()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
    .padding(16)
  }
}

调用

import { MyDialog } from './component/MyDialog';

@Entry
@Component
struct Page {

  @State message: string = 'Hello World';

  build() {
    Column() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          this.dialogController.open();
        })
    }
    .height('100%')
    .width('100%')
  }

  // 自定义弹窗
  dialogController: CustomDialogController = new CustomDialogController({
    builder: MyDialog({
      title: '标题',
      cancelBtnText: '取消',
      confirmBtnText: '确定',
      message: '这是弹窗内容',
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() }
    })
  })

  onCancel() {
    console.log('取消');
  }

  onAccept() {
    console.log('确定');
  }
}
标签: 暂无
最后更新:2025-09-09

dsttl3

https://dsttl3.xyz

点赞
下一篇 >

© 2025 dsttl3.xyz

Theme Kratos Made By Seaton Jiang