鸿蒙UI系统组件08——自定义弹窗(CustomDialog)
蓝亭书序 2026-08-13 18:00:32 发布0 浏览 0 点赞 0 收藏
1、概述
自定义弹窗(CustomDialog)可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗。
2、创建自定义弹窗
- 使用@CustomDialog装饰器装饰自定义弹窗。
- @CustomDialog装饰器用于装饰自定义弹框,此装饰器内进行自定义内容(也就是弹框内容
@CustomDialog
struct CustomDialogExample {
controller: CustomDialogController
build() {
Column() {
Text('我是内容')
.fontSize(20)
.margin({ top: 10, bottom: 10 })
}
}
}3. 创建构造器,与装饰器呼应相连。
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({}),
})4. 点击与onClick事件绑定的组件使弹窗弹出
Flex({justifyContent:FlexAlign.Center}){
Button('click me')
.onClick(() => {
this.dialogController.open()
})
}.width('100%')
3、弹窗的交互
弹窗可用于数据交互,完成用户一系列响应操作。
- 在@CustomDialog装饰器内添加按钮操作,同时添加数据函数的创建。
@CustomDialog
struct CustomDialogExample {
controller: CustomDialogController
cancel: () => void
confirm: () => void
build() {
Column() {
Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller.close()
this.cancel()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
this.controller.close()
this.confirm()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}2. 页面内需要在构造器内进行接收,同时创建相应的函数操作。
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({
cancel: this.onCancel,
confirm: this.onAccept,
}),
alignment: DialogAlignment.Default, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示
})
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
4、一个完整的Demo
// xxx.ets
@CustomDialog
struct CustomDialogExample {
controller: CustomDialogController
cancel: () => void
confirm: () => void
build() {
Column() {
Text('我是内容').fontSize(20).margin({ top: 10, bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller.close()
this.cancel()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
this.controller.close()
this.confirm()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}
@Entry
@Component
struct DialogExample {
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({
cancel: this.onCancel,
confirm: this.onAccept,
}),
alignment: DialogAlignment.Default, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示
})
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
build() {
Flex({ justifyContent: FlexAlign.Center }) {
Button('click me')
.onClick(() => {
this.dialogController.open()
})
}.width('100%')
}
}
©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS
标签
CustomDialog
Demo
鸿蒙UI系统
相关推荐
鸿蒙UI系统组件07——视频播放器(Video)
蓝亭书序
0
0鸿蒙UI系统组件06——进度条(Progress)
蓝亭书序
0
0鸿蒙UI系统组件05——切换按钮(Toggle)
蓝亭书序
0
0鸿蒙UI系统组件04——单选框(Radio)
蓝亭书序
1
0鸿蒙UI系统组件03——文本输入(TextInput/TextArea)
蓝亭书序
0
0
蓝亭书序
我还没有写个人简介......
28
帖子
0
提问
0
粉丝
最新发布
鸿蒙UI系统组件02——按钮(Button)
2026-08-07 14:42:01 发布鸿蒙UI系统组件01——文本组件(Text/Span)
2026-08-07 14:41:18 发布热门推荐
京公网安备:11010502051901号