往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)
介绍
弹出模态框,常用于消息提示、消息确认
导入依赖
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)
参数
参数名 | 参数类型 | 名称 |
---|---|---|
title | string | 设置标题,默认值为 "" |
message | string | 设置内容区域,默认值为 "" |
leftText | string | 设置左边按钮文本,默认值为 "返回" |
leftTextColor | ResourceColor | 设置左边按钮文本的字体颜色,默认值为$r("app.color.color_272a2b") |
rightText | string | 设置右边按钮文本,默认值为 "确认" |
rightTextClick | () => void | 设置右边按钮的点击事件,默认为空实现 |
rightTextColor | ResourceColor | 设置右边按钮文本的字体颜色,默认值为$r("app.color.color_194d53") |
rightTextBold | boolean | 右边按钮字体是否加粗,默认值为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参数
参数名 | 参数类型 | 名称 |
---|---|---|
title | string | 设置标题,默认值为 "" |
message | string | 设置内容区域,默认值为 "" |
leftText | string | 设置左边按钮文本,默认值为 "返回" |
leftTextColor | ResourceColor | 设置左边按钮文本的字体颜色,默认值为$r("app.color.color_272a2b") |
leftTextClick | () => void | 设置左边按钮的点击事件,默认为空实现 |
rightText | string | 设置右边按钮文本,默认值为 "确认" |
rightTextClick | () => void | 设置右边按钮的点击事件,默认为空实现 |
rightTextColor | ResourceColor | 设置右边按钮文本的字体颜色,默认值为$r("app.color.color_194d53") |
rightTextBold | boolean | 右边按钮字体是否加粗,默认值为true,表示加粗 |
3. 设置标题
setTitle(title: string = ""):void
参数
参数名 | 参数类型 | 名称 |
---|---|---|
title | string | 设置标题,默认值为 "" |
4. 设置内容信息
setMessage(message: string = "") :void
参数
参数名 | 参数类型 | 名称 |
---|---|---|
message | string | 设置内容区域,默认值为 "" |
5. 设置左边按钮
setLeftButton(leftText: string = "返回", leftTextClick: () => void = () => {})
参数
参数名 | 参数类型 | 名称 |
---|---|---|
leftText | string | 设置左边按钮文本,默认值为 "返回" |
leftTextClick | () => void | 设置左边按钮的点击事件,默认为空实现 |
6. 设置右边按钮
setRightButton(rightText: string = "确认", rightTextClick: () => void = () => {})
参数
参数名 | 参数类型 | 名称 |
---|---|---|
rightText | string | 设置右边按钮文本,默认值为 "确认" |
rightTextClick | () => void | 设置右边按钮的点击事件,默认为空实现 |