HarmonyOS Dialog点击其他位置不消失或者Toast支持自定义CustomBuilder

HarmonyOS Dialog点击其他位置不消失或者Toast支持自定义CustomBuilder。

HarmonyOS
2024-09-27 13:25:43
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可以参考如下开发代码实现:

import { BusinessError } from '@ohos.base';  
import { ComponentContent } from "@ohos.arkui.node";  
import WindowModel from '../common/utils/WindowModel'  
import { PromptAction } from '@kit.ArkUI';  
export class Params {  
  text: string = ""  
  constructor(text: string) {  
    this.text = text;  
  }  
}  
let promptAction: PromptAction | undefined = undefined;  
let contentNodeGlobale: ComponentContent<Params> | undefined = undefined;  
function myFunc(contentNode: ComponentContent<Params>, uiContext?: UIContext) {  
  let uiContextFromWindow = WindowModel.getInstance().getWindowClass()?.getUIContext();  
  if (uiContextFromWindow) {  
    promptAction = uiContextFromWindow.getPromptAction();  
    try {  
      contentNodeGlobale = contentNode;  
      promptAction.openCustomDialog(contentNode, {  
        alignment: DialogAlignment.Center,  
        showInSubWindow: true,  
        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()  
          }  
        }  
      });  
      console.log(,'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() {  
    Text(params.text)  
      .fontSize(50)  
      .fontWeight(FontWeight.Bold)  
      .margin({ bottom: 36 })  
    Button('Close')  
      .onClick(() => {  
        promptAction?.closeCustomDialog(contentNodeGlobale)  
      })  
  }.backgroundColor('#FFF0F0F0')  
}  
@Entry  
@Component  
struct Index {  
  @State message: string = "hello"  
  uiContext = this.getUIContext();  
  contentNode = new ComponentContent(this.uiContext, wrapBuilder(buildText), new Params(this.message));  
  build() {  
    Row() {  
      Column() {  
        Button("click me")  
          .onClick(() => {  
            myFunc(this.contentNode);  
            setTimeout(() => {  
              this.contentNode?.update(new Params("new message"));  
            }, 2000);   
          })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
分享
微博
QQ
微信
回复
2024-09-27 17:32:07
相关问题
HarmonyOS 如何自定义toast
952浏览 • 1回复 待解决
HarmonyOS 如何自定义 toast 样式?
1031浏览 • 1回复 待解决
HarmonyOS 如何自定义toast组件
907浏览 • 1回复 待解决
HarmonyOS 如何实现自定义Toast
685浏览 • 1回复 待解决
HarmonyOS 自定义全屏dialog
909浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
854浏览 • 1回复 待解决
HarmonyOS 自定义全局dialog
712浏览 • 1回复 待解决
鸿蒙组件toast自定义样式
9835浏览 • 1回复 待解决
HarmonyOS 用CustomDialog自定义Dialog
1149浏览 • 1回复 待解决
HarmonyOS 自定义Dialog高度问题
801浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
1273浏览 • 1回复 待解决