中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
HarmonyOS 非ui界面拉起自定义弹窗的demo。
微信扫码分享
//index import { ComponentContent } from "@ohos.ArkUI.node"; import { myFunc, Params } from './CustomDialogController' import { PromptAction, router } from '@kit.ArkUI'; let PromptActionGlobal: PromptAction | undefined = undefined; let ContentNodeGlobal: ComponentContent<Params> | undefined = undefined; const params: boolean = router.getParams() as boolean; @Builder function buildText(params: Params) { Column() { test() }.backgroundColor('#FFF0F0F0') } @Component struct test{ controller: RichEditorController = new RichEditorController() build() { Column() { RichEditor({ controller: this.controller }) // 绑定自定义键盘 // .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) .height(200) .id('ni') .borderWidth(1) .borderColor(Color.Red) .width("100%") .onAppear(()=>{ focusControl.requestFocus('ni') }) .onFocus(()=>{ console.log('聚焦') }) .onBlur(()=>{ console.log('失去聚焦') }) } } } @Entry @Component struct CustomDialogDemo { @State message: string = "hello" uiContext = this.getUIContext(); isUpdata: boolean = false; aboutToAppear() { PromptActionGlobal = this.uiContext.getPromptAction(); ContentNodeGlobal = new ComponentContent(this.uiContext, wrapBuilder(buildText), new Params(this.message)); } build() { Row() { Column() { Button("click me") .onClick(() => { if (ContentNodeGlobal) { myFunc(ContentNodeGlobal, this.uiContext); } }) } .width('100%') .height('100%') } .height('100%') } } //dialogController import { BusinessError } from '@ohos.base'; import { ComponentContent } from "@ohos.ArkUI.node"; import { UIContext } from '@kit.ArkUI'; export class Params { text: string = "" constructor(text: string) { this.text = text; } } declare enum DialogAlignment { Top, Center, Bottom, Default, TopStart, TopEnd, CenterStart, CenterEnd, BottomStart, BottomEnd } declare enum DismissReason { PRESS_BACK = 0, TOUCH_OUTSIDE = 1, CLOSE_BUTTON = 2 } export function myFunc(contentNode: ComponentContent<Params>, uiContext: UIContext) { let promptAction = uiContext.getPromptAction(); try { promptAction.openCustomDialog(contentNode, { alignment: DialogAlignment.Center, showInSubWindow: true, // 作为子窗口弹出,而不是toast的形式 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { console.info("reason" + JSON.stringify(dismissDialogAction.reason)) console.log("MWB ", "dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } }); console.log('MWB', 'Start to openCustomDialog') } catch (error) { let message = (error as BusinessError).message; let code = (error as BusinessError).code; console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); }; } import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } let windowClass: window.Window | undefined = undefined; windowStage.getMainWindow((err: BusinessError, data) => { let errCode: number = err.code; if (errCode) { console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); return; } windowClass = data; console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); // 获取UIContext实例。 let uiContext: UIContext | null = null; uiContext = windowClass.getUIContext(); AppStorage.setOrCreate('uiContext1111',uiContext) }) hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(0x0000, 'wangyi testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { } } import { myFunc, Params } from './CustomDialogController' @Entry @Component struct CustomDialogDemo { @State message: string = "hello" aboutToAppear(): void { myFunc(); } build() { Row() { Column() { Button("click me") .onClick(() => { }) } .width('100%') .height('100%') } .height('100%') } } //dialogController import { BusinessError } from '@ohos.base'; import { ComponentContent } from "@ohos.ArkUI.node"; import { UIContext, window } from '@kit.ArkUI'; export class Params { text: string = "" constructor(text: string) { this.text = text; } } declare enum DialogAlignment { Top, Center, Bottom, Default, TopStart, TopEnd, CenterStart, CenterEnd, BottomStart, BottomEnd } declare enum DismissReason { PRESS_BACK = 0, TOUCH_OUTSIDE = 1, CLOSE_BUTTON = 2 } export function myFunc() { let uiContext: UIContext | undefined = AppStorage.get('uiContext1111'); console.info('wangyi ' + uiContext) try { if (uiContext) { let promptAction = uiContext.getPromptAction(); promptAction.openCustomDialog(new ComponentContent(uiContext, wrapBuilder(buildText), new Params('as')), { alignment: DialogAlignment.Center, showInSubWindow: true, // 作为子窗口弹出,而不是toast的形式 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { console.info("reason" + JSON.stringify(dismissDialogAction.reason)) console.log("MWB ", "dialog onWillDismiss") if (dismissDialogAction.reason == DismissReason.PRESS_BACK) { dismissDialogAction.dismiss() } if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) { dismissDialogAction.dismiss() } } }); console.log('MWB', 'Start to openCustomDialog') } } catch (error) { let message = (error as BusinessError).message; let code = (error as BusinessError).code; console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); } ; } @Builder function buildText(params: Params) { Column() { test() }.backgroundColor('#FFF0F0F0') } @Component struct test { controller: RichEditorController = new RichEditorController() build() { Column() { RichEditor({ controller: this.controller })// 绑定自定义键盘 // .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) .height(200) .id('ni') .borderWidth(1) .borderColor(Color.Red) .width("100%") .onAppear(() => { focusControl.requestFocus('ni') }) .onFocus(() => { console.log('聚焦') }) .onBlur(() => { console.log('失去聚焦') }) } } }