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%')  
  }  
}
分享
微博
QQ
微信
回复
2024-09-27 17:32:07
相关问题
HarmonyOS 用CustomDialog自定义Dialog
169浏览 • 1回复 待解决
HarmonyOS 自定义dialog open无效
165浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
341浏览 • 1回复 待解决
鸿蒙组件toast自定义样式
8668浏览 • 1回复 待解决
希望Tabs的页签位置是否支持自定义
225浏览 • 1回复 待解决
如何设置自定义弹窗位置
1970浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景色透明问题
769浏览 • 1回复 待解决
HarmonyOS UI组件自定义点击范围
221浏览 • 1回复 待解决
鸿蒙怎么实现自定义布局的Dialog
9098浏览 • 2回复 已解决