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

HarmonyOS
2024-12-25 13:03:25
浏览
收藏 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%')
  }
}
  • 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.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
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%')
  }
}
  • 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.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
分享
微博
QQ
微信
回复
2024-12-25 15:32:23
相关问题
promptAction.openCustomDialog 自定义弹窗
896浏览 • 1回复 待解决
HarmonyOS promptAction.openCustomDialog问题
642浏览 • 1回复 待解决
promptAction.openCustomDialog 全局弹窗
1353浏览 • 1回复 待解决
HarmonyOS 背景半透明渐变怎么设置
933浏览 • 1回复 待解决
HarmonyOS 如何设置页面背景半透明
744浏览 • 1回复 待解决