社区运营|
HarmonyOS 5
如何封装一个自定义Dialog对话框
如何封装一个自定义Dialog对话框
2
浏览582 发布于2024-03-19 06:43广东
全部评论
最多点赞
最新发布
最早发布

1.首先自定义Dialog组件:
收起
自动换行
深色代码主题
复制
@CustomDialog
export default struct CustomDialogExample {
@Link textValue: string;
@Link inputValue: string;
controller: CustomDialogController;
cancel: () => void = () => {};
confirm: () => void = () => {};
build() {
Column() {
Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
.onChange((value: string) => {
this.textValue = value
})
Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('cancel')
.onClick(() => {
this.controller.close()
this.cancel()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('confirm')
.onClick(() => {
this.inputValue = this.textValue
this.controller.close()
this.confirm()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}
2.在需要用到Dialog的组件中使用:
收起
自动换行
深色代码主题
复制
import CustomDialogExample from './CustomDialogExample';
@Entry
@Component
struct CustomDialogUser {
@State textValue: string = '';
@State inputValue: string = 'click me';
//配置自定义弹窗的参数
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample({
cancel: this.onCancel,
confirm: this.onAccept,
textValue: $textValue,
inputValue: $inputValue
}),
cancel: this.existApp,
autoCancel: true,
alignment: DialogAlignment.Default,
offset: { dx: 0, dy: -20 },
gridCount: 4,
customStyle: false
});
// 在自定义组件即将析构销毁时将dialogController置空
aboutToDisappear() {
this.dialogController = null
}
onCancel() {
console.info('Callback when the first button is clicked')
}
onAccept() {
console.info('Callback when the second button is clicked')
}
existApp() {
console.info('Click the callback in the blank area')
}
build() {
Column() {
Button(this.inputValue)
.onClick(() => {
if (this.dialogController != null) {
this.dialogController.open()
}
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
参考链接
展开全部 
1楼回复于2024-03-19 06:44 来自广东
anonymous怎么解决返回无法拦截的问题 啊2024-03-27 01:36 来自四川
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
相关推荐
换一批
1
HarmonyOS 自定义TurboModule或者Fabric组件,封装使用流程 (API12+)
2
HarmonyOS 封装后,CustomDialogController open不起作用(API12+)
3
HarmonyOS AudioCapturer是否支持封装wav格式?(API12+)
4
HarmonyOS CustomContentDialog如何设置在点击对话框外的内容时,对话框不会自动关闭(API12+)
5
HarmonyOS 如何将dialog封装成一个工具类?(API12+)
6
HarmonyOS CustomDialog优先级如何设置 (API12+)
7
HarmonyOS CustomDialog自定义弹窗如何取消遮罩层或者变为完全透明状态?(API12+)
8
CustomDialog自定义弹窗标题样式设置方法
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号