HarmonyOS 使用promptAction.opencustomdialog怎么自定义半透明背景的颜色、控制关闭

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

示例参考如下:

import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.arkui.node";
import { PromptAction } from '@kit.ArkUI';

//弹框属性
export class Params {
  text: string = ""
  backgroundColor: string = ""
  opacity: number = 1
  width: number = 0
  height: number = 0
  btnControl: string = ''
}

//自定义弹框
@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text || '默认内容')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
  }
  .width(params.width || 200)
  .height(params.height || 400)
  .backgroundColor(params.backgroundColor || '#FFF0F0F0')
  .opacity(params.opacity)
}


//弹框控制
export function setDialog(uiContext: UIContext, promptAction: PromptAction, params: Params) {
  let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), params);
  try {
    promptAction.openCustomDialog(contentNode, {
      onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
        if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
          console.log('左右滑事件:')
          if (params.btnControl !== '关闭控制') {
            dismissDialogAction.dismiss()
          }
        }
        if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
          console.log('点击遮罩层事件:',)
          if (params.btnControl !== '关闭控制') {
            dismissDialogAction.dismiss()
          }
        }
      }
    });
  } 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}`);
  }
}

@Entry
@Component
struct Index {
  @State message: string = "hello"
  private uiContext = this.getUIContext()
  private promptAction = this.getUIContext().getPromptAction()

  build() {
    Row() {
      Column() {
        Button("弹出弹框")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("设置背景颜色")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.backgroundColor = '#ffff0000'
            params.opacity = 0.6
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("设置弹框大小")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.width = 300
            params.height = 500
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("控制弹框关闭")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.btnControl = '关闭控制'
            setDialog(this.uiContext, this.promptAction, params)
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.arkui.node";
import { PromptAction, window } from '@kit.ArkUI';

//弹框属性
export class Params {
  text: string = "" //弹框文本内容
  backgroundColor: string = "" //弹框颜色
  opacity: number = 1 //弹框透明度
  width: number = 0 //宽度
  height: number = 0 //高度
  btnControl: string = '' //弹框控制
  maskColor: string = '' //蒙层颜色'rgba(255, 100, 255, 0.5)'
}

//自定义弹框
@Builder
export function buildText(params: Params) {
  Column() {
    Text(params.text || '默认内容')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
  }
  .width(params.width || 200)
  .height(params.height || 400)
  .backgroundColor(params.backgroundColor || '#FFF0F0F0')
  .opacity(params.opacity)
}

//弹框控制
export function setDialog(uiContext: UIContext, promptAction: PromptAction, params: Params) {
  let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), params);
  AppStorage.setOrCreate('contentNode', contentNode)
  try {
    promptAction.openCustomDialog(contentNode, {
      //BaseDialogOptions属性:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#basedialogoptions11
      maskColor: params.maskColor,
      onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
        if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
          console.log('左右滑事件:')
          if (params.btnControl !== '关闭控制') {
            dismissDialogAction.dismiss()
          }
        }
        if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
          console.log('点击遮罩层事件:',)
          if (params.btnControl !== '关闭控制') {
            dismissDialogAction.dismiss()
          }
        }
      }
    });
  } 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}`);
  }
}

@Entry
@Component
struct Index {
  @State message: string = "hello"
  private uiContext = this.getUIContext()
  private promptAction = this.getUIContext().getPromptAction()

  aboutToAppear(): void {
    AppStorage.setOrCreate('promptAction', this.promptAction)
  }

  build() {
    Row() {
      Column() {
        Button("弹出弹框")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("设置背景颜色")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.backgroundColor = '#ffff0000'
            params.opacity = 0.6
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("设置弹框大小")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.width = 300
            params.height = 500
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("设置弹框蒙层颜色")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.maskColor = 'rgba(255, 100, 255, 0.5)'
            setDialog(this.uiContext, this.promptAction, params)
          })

        Button("控制弹框关闭")
          .margin(10)
          .width('80%')
          .onClick(() => {
            let params: Params = new Params()
            params.btnControl = '关闭控制'
            setDialog(this.uiContext, this.promptAction, params)
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
promptAction.openCustomDialog 自定义弹窗
362浏览 • 1回复 待解决
promptAction.openCustomDialog 全局弹窗
501浏览 • 1回复 待解决
HarmonyOS 如何设置页面背景半透明
40浏览 • 1回复 待解决
自定义颜色透明度如何实现?
392浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景透明问题
1002浏览 • 1回复 待解决