HarmonyOS 非ui界面拉起自定义弹窗的demo

HarmonyOS 非ui界面拉起自定义弹窗的demo。

HarmonyOS
2024-11-26 09:26:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280
//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('失去聚焦') 
        }) 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-11-26 16:06:52
相关问题
HarmonyOS 全局自定义弹窗demo
286浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1133浏览 • 1回复 待解决
HarmonyOS 自定义弹窗问题
694浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
356浏览 • 1回复 待解决
HarmonyOS 自定义相机演示demo
207浏览 • 1回复 待解决
HarmonyOS 自定义相机demo
272浏览 • 1回复 待解决
hvigor自定义扩展demo
1005浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
612浏览 • 1回复 待解决
HarmonyOS 自定义弹窗遮罩未全屏
665浏览 • 1回复 待解决
如何在自定义弹窗中再次弹窗
2268浏览 • 1回复 待解决
自定义弹窗使用相关问题
922浏览 • 1回复 待解决
自定义弹窗如何嵌套使用
1676浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog)问题
396浏览 • 1回复 待解决
HarmonyOS 自定义弹窗封装后不显示
341浏览 • 1回复 待解决