当前位置:首页 > 未命名 > 正文内容

鸿蒙next自定义弹窗

dsttl33个月前 (06-01)未命名508

自定义弹窗组件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('确定');
  }
}

扫描二维码推送至手机访问。

版权声明:本文由dsttl3发布,如需转载请注明出处。

本文链接:https://dsttl3.cn/?id=494

分享给朋友:
返回列表

上一篇:鸿蒙next长按事件

没有最新的文章了...