【入门到实战】OpenHarmony开发:Dialog 弹出框

往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)


介绍

弹出模态框,常用于消息提示、消息确认

Dialog 弹出框

导入依赖

import { DialogComponent } from '@peakmain/library/Index';

1. body设置参数

  setBodyMessage(title: string = "",
    message: string = "",
    leftText: string = "返回", //左边按钮
    leftTextColor: ResourceColor = $r("app.color.color_272a2b"), //左边文本颜色
    leftTextClick: () => void = () => {
    },
    rightText: string = "确认",
    rightTextColor: ResourceColor = $r("app.color.color_194d53"),
    rightTextClick: () => void = () => {
    },
    rightTextBold: boolean = true)
参数
参数名参数类型名称
titlestring设置标题,默认值为 ""
messagestring设置内容区域,默认值为 ""
leftTextstring设置左边按钮文本,默认值为 "返回"
leftTextColorResourceColor设置左边按钮文本的字体颜色,默认值为$r("app.color.color_272a2b")
rightTextstring设置右边按钮文本,默认值为 "确认"
rightTextClick() => void设置右边按钮的点击事件,默认为空实现
rightTextColorResourceColor设置右边按钮文本的字体颜色,默认值为$r("app.color.color_194d53")
rightTextBoldboolean右边按钮字体是否加粗,默认值为true,表示加粗
示例代码
import { promptAction } from '@kit.ArkUI';
import { DialogComponent, NavBar } from '@peakmain/library';

@Entry
@Component
struct DialogPage {
  title: string = '确认删除订单';
  message: string = '删除后将无法恢复,无法再次操作。';
  leftText: string = '取消';
  rightTextColor: ResourceColor = $r("app.color.color_194d53")
  rightTextBold: boolean = true

  resetDefault() {
    this.title = '确认删除订单';
    this.message = '删除后将无法恢复,无法再次操作。';
    this.leftText = '取消';
    this.rightTextColor = $r("app.color.color_194d53")
    this.rightTextBold = true
  }

  confirm: CustomDialogController = new CustomDialogController({
    builder: DialogComponent({
      title: this.title,
      message: this.message,
      leftText: this.leftText,
      rightTextColor: this.rightTextColor,
      rightTextBold: this.rightTextBold,
      rightTextClick: () => {
        promptAction.showToast({
          message: `点击了${this.title}`,
        })
        this.confirm.close()
        this.resetDefault()
      }
    }),
    customStyle: true,
    alignment: DialogAlignment.Center,

  })

  build() {
      Column({ space: 20 }) {
        NavBar({
          title:"Dialog 弹出框"
        })
        Button("默认弹窗")
          .onClick(() => {
            this.confirm.open()
          })
        Button("提示弹窗")
          .onClick(() => {
            this.title = "提示"
            this.leftText = ""
            this.rightTextColor = $r("app.color.color_1989fa")
            this.confirm.open()
          })
        Button("弹窗(无标题)")
          .onClick(() => {
            this.title = ""
            this.leftText = ""
            this.rightTextColor = $r("app.color.color_1989fa")
            this.confirm.open()
          })

      }
      .width('100%')
      .height("100")
  }
}

2. build设置参数

setBuilderMessage(builder: DialogBuilder):void

DialogBuilder参数
参数名参数类型名称
titlestring设置标题,默认值为 ""
messagestring设置内容区域,默认值为 ""
leftTextstring设置左边按钮文本,默认值为 "返回"
leftTextColorResourceColor设置左边按钮文本的字体颜色,默认值为$r("app.color.color_272a2b")
leftTextClick() => void设置左边按钮的点击事件,默认为空实现
rightTextstring设置右边按钮文本,默认值为 "确认"
rightTextClick() => void设置右边按钮的点击事件,默认为空实现
rightTextColorResourceColor设置右边按钮文本的字体颜色,默认值为$r("app.color.color_194d53")
rightTextBoldboolean右边按钮字体是否加粗,默认值为true,表示加粗

3. 设置标题

setTitle(title: string = ""):void

参数
参数名参数类型名称
titlestring设置标题,默认值为 ""

4. 设置内容信息

setMessage(message: string = "") :void

参数
参数名参数类型名称
messagestring设置内容区域,默认值为 ""

5. 设置左边按钮

setLeftButton(leftText: string = "返回", leftTextClick: () => void = () => {})

参数
参数名参数类型名称
leftTextstring设置左边按钮文本,默认值为 "返回"
leftTextClick() => void设置左边按钮的点击事件,默认为空实现

6. 设置右边按钮

setRightButton(rightText: string = "确认", rightTextClick: () => void = () => {})

参数
参数名参数类型名称
rightTextstring设置右边按钮文本,默认值为 "确认"
rightTextClick() => void设置右边按钮的点击事件,默认为空实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值