【ArkUI弹出框系列】六:弹出框(Dialog)控制器
ArkUI的弹出框控制器在绑定弹出框后,可提供对弹出框的操作能力,当前支持关闭和获取状态功能。可以将控制器传入弹出框内容区域后进行操作,也可以在内容区域直接获取控制器。
当前弹出框控制器仅支持绑定自定义弹出框,一个弹出框控制器只能绑定一个弹出框,且操作只对该弹出框生效。
使用 getDialogController 获取弹出框控制器时,如果当前自定义组件不在弹出框中显示则获取为undefined。
一、弹出框绑定控制器
可以先初始化一个弹出框控制器,然后在调用弹出框接口时通过传入 dialogController: promptAction.CommonController参数去绑定弹出框,绑定成功后就可以操作弹出框。如果同一个弹出框控制器绑定不同的弹出框,那么只能操作最后一次绑定的弹出框。
这里通过openCustomDialogWithController接口实现一个绑定控制器的弹出框的样例代码:
class Params {
text: string = ""
dialogController: promptAction.DialogController = new promptAction.DialogController()
constructor(text: string, dialogController: promptAction.DialogController) {
this.text = text
this.dialogController = dialogController
}
}
@Builder
function BuilderDialog(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
Button('Close CustomDialog')
.onClick(() => {
if (params.dialogController != undefined) {
params.dialogController.close();
}
})
}
}
@Entry
@Component
struct DialogSample {
private message: string = 'CustomDialog';
private dialogController: promptAction.DialogController = new promptAction.DialogController();
build() {
Column() {
Button('openCustomDialog')
.onClick(() => {
const contentNode = new ComponentContent<Params>(
this.getUIContext(), wrapBuilder(BuilderDialog), new Params(this.message, this.dialogController));
this.getUIContext().getPromptAction().openCustomDialogWithController(contentNode, this.dialogController);
})
}
.width('100%')
}
}
这里通过presentCustomDialog接口实现一个绑定控制器的弹出框的样例代码:
@Entry
@Component
struct DialogSample {
private message: string = "CustomDialog";
private dialogController: promptAction.DialogController = new promptAction.DialogController();
private customDialogComponentId: number = 0;
@Builder customDialogComponent() {
Column() {
Text(this.message).fontSize(30)
Row({ space: 10 }) {
Button('Close CustomDialog').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentId);
})
Button('Close CustomDialog').onClick(() => {
this.dialogController.close();
})
}
}.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween)
}
@Builder customDialogComponentWithId(dialogId: number) {
Column() {
Text(this.message).fontSize(30)
Row({ space: 10 }) {
Button('Close CustomDialog').onClick(() => {
this.promptAction.closeCustomDialog(dialogId);
})
Button('Close CustomDialog').onClick(() => {
this.dialogController.close();
})
}
}.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween)
}
build() {
Column() {
Button('presentCustomDialog')
.fontSize(20)
.onClick(() => {
this.promptAction.presentCustomDialog(() => {
this.customDialogComponent()
}, this.dialogController)
.then((dialogId: number) => {
this.customDialogComponentId = dialogId;
})
.catch((err: BusinessError) => {
console.error("presentCustomDialog error: " + err.code + " " + err.message);
})
})
Button('presentCustomDialog with id')
.fontSize(20)
.onClick(() => {
this.promptAction.presentCustomDialog((dialogId: number) => {
this.customDialogComponentWithId(dialogId)
}, this.dialogController)
.catch((err: BusinessError) => {
console.error("presentCustomDialog with id error: " + err.code + " " + err.message);
})
})
}
.width('100%')
}
}
二、内容区绑定控制器
可以在自定义组件中通过 getDialogController: promptAction.CommonController接口获取一个弹出框控制器,如果自定义组件为某个弹出框的内容区,那么控制器会直接绑定该弹出框,否则直接获取到的控制器为undefined。
这里通过openCustomDialog接口实现一个内容区获取并绑定控制器的弹出框的样例代码:
class Params {
text: string = ""
constructor(text: string) {
this.text = text
}
}
@Builder
function BuilderDialog(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
Button('Close CustomDialog')
.onClick(() => {
let controller = this.getDialogController();
if (controller !== undefined) {
controller.close()
}
}
}
}
@Entry
@Component
struct DialogSample {
private message: string = 'CustomDialog';
build() {
Column() {
Button('openCustomDialog')
.onClick(() => {
const contentNode = new ComponentContent<Params>(
this.getUIContext(), wrapBuilder(BuilderDialog), new Params(this.message));
this.getUIContext().getPromptAction().openCustomDialog(contentNode);
})
}
.width('100%')
}
}
1
浏览8 编辑于昨天06:30上海
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
目录 收起
一、弹出框绑定控制器
二、内容区绑定控制器
了解社区公约,与您携手共创和谐专业的开发者社区。





































苏公安网备 32011402010933号