HarmonyOS 鸿蒙Next 关于openCustomDialog方法
HarmonyOS 鸿蒙Next 关于openCustomDialog方法 我在拦截器中做测试 无法弹出这个弹窗,这是因为什么原因呢
import { promptAction } from '@kit.ArkUI'
let customDialogId: number = 0
@Builder
export function buildText() {
Column() {
Text('Custom dialog Message').fontSize(10)
Row() {
Button("确认").onClick(() => {
promptAction.closeCustomDialog(customDialogId)
})
Blank().width(50)
Button("取消").onClick(() => {
promptAction.closeCustomDialog(customDialogId)
})
}
}.height(200).padding(5)
} //自定义组件的内容
axiosClient.interceptors.response.use(
async (response: AxiosResponse) => {
let windowClass = await window.getLastWindow(getContext())
let uiContext = windowClass.getUIContext()
let promptAction = uiContext.getPromptAction();
let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), 'data.message');
promptAction.openCustomDialog(contentNode, {
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
console.info("reason" + JSON.stringify(dismissDialogAction.reason))
console.log("dialog onWillDismiss")
}
})
}
)
更多关于HarmonyOS 鸿蒙Next 关于openCustomDialog方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你需要手动获取UIContext,示例如下:
1、创建一个全局Context,GlobalContext.etx:
export class GlobalContext {
private constructor() {
}
private static instance: GlobalContext;
private _objects = new Map<string, Object>();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}
2、在【EntryAbility.ets --> onWindowStageCreate】中,获取主窗口:
GlobalContext.getContext().setObject("windowStage", windowStage);
3、调用:
let windowStage = GlobalContext.getContext().getObject('windowStage') as window.WindowStage
windowStage.getMainWindowSync().getUIContext().getPromptAction().showToast({
message: '请求失败'
});
【api12已经没有customDialogId这个参数方法了】关闭弹窗请参考下面的demo:
import { promptAction } from '@kit.ArkUI'
@Entry
@Component
struct Index {
private customDialogComponentId: number = 0
@Builder
customDialogComponent() {
Column() {
Text('弹窗').fontSize(30)
Row({ space: 50 }) {
Button('确认').onClick(() => {
promptAction.closeCustomDialog(this.customDialogComponentId)
})
Button('取消').onClick(() => {
promptAction.closeCustomDialog(this.customDialogComponentId)
})
}
}.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween)
}
build() {
Row() {
Column({ space: 20 }) {
Text('组件内弹窗')
.fontSize(30)
.onClick(() => {
promptAction.openCustomDialog({
builder: () => {
this.customDialogComponent()
},
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
console.info('reason' + JSON.stringify(dismissDialogAction.reason))
console.log('dialog onWillDismiss')
if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss()
}
if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
dismissDialogAction.dismiss()
}
}
}).then((dialogId: number) => {
this.customDialogComponentId = dialogId
})
})
}
.width('100%')
}
.height('100%')
}
}
参考以下完整demo:
import { ComponentContent, promptAction, window } from '@kit.ArkUI';
class Params {
text: string = ''
constructor(text: string) {
this.text = text;
}
}
let contentNode = new Object();
@Builder
function buildText(params: Params) {
Column() {
Text(params.text).fontSize(30)
Row() {
Button('确认').onClick(() => {
window.getLastWindow(getContext()).then((windowClass) => {
let uiContext = windowClass.getUIContext()
let promptAction = uiContext.getPromptAction();
promptAction.closeCustomDialog(contentNode as ComponentContent<number>)
})
})
Button('取消').onClick(() => {
window.getLastWindow(getContext()).then((windowClass) => {
let uiContext = windowClass.getUIContext()
let promptAction = uiContext.getPromptAction();
promptAction.closeCustomDialog(contentNode as ComponentContent<number>)
})
})
}
}.height(200).padding(5).justifyContent(FlexAlign.SpaceBetween).backgroundColor(Color.Red)
}
@Entry
@Component
struct Index {
@State message: string = 'hello'
build() {
Row() {
Column() {
Button('click me')
.onClick(() => {
let uiContext = this.getUIContext();
let promptAction = uiContext.getPromptAction();
contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));
promptAction.openCustomDialog(contentNode as ComponentContent<number>)
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next 关于openCustomDialog方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
关于HarmonyOS(鸿蒙)Next中的openCustomDialog
方法,这是一个用于显示自定义对话框的API。在鸿蒙系统中,对话框是一种常见的用户界面组件,用于向用户显示信息或获取用户输入。
openCustomDialog
方法通常允许开发者指定对话框的布局、标题、内容以及按钮等。使用此方法时,你需要提供一个自定义的对话框布局资源,该资源定义了对话框的外观和内容。此外,你还可以设置对话框的行为,比如是否可取消、点击外部区域是否关闭对话框等。
在调用openCustomDialog
方法时,通常会返回一个对话框实例或相关的控制器对象,允许你进一步操作对话框,比如更新内容、设置按钮点击事件等。
需要注意的是,具体如何使用openCustomDialog
方法,包括其参数和返回值类型,可能会根据鸿蒙系统的版本和API的更新而有所变化。因此,建议查阅最新的鸿蒙系统开发者文档或API参考来获取准确的信息。
如果你在使用openCustomDialog
方法时遇到问题,比如对话框无法正确显示或行为不符合预期,请检查你的对话框布局和资源文件是否正确配置,以及你的调用代码是否符合API的要求。如果问题依旧没法解决请联系官网客服,官网地址是:
https://www.itying.com/category-93-b0.html