HarmonyOS鸿蒙Next中如何使用自定义弹窗实现分享弹窗

HarmonyOS鸿蒙Next中如何使用自定义弹窗实现分享弹窗 可以使用promptAction结合ComponentContent实现自定义分享弹窗。示例代码如下:

import { ComponentContent } from '@kit.ArkUI';


let componentContent: ComponentContent<Params> | undefined = undefined;


class Params {
  applicationSharings: string[] = [];
  sharings: string[] = [];


  constructor(applicationSharings: string[], sharings: string[] = []) {
    this.applicationSharings = applicationSharings;
    this.sharings = sharings;
  }
}


const context = AppStorage.get("context") as UIContext


@Builder
function  buildText($$: Params) {
  Column() {
    Text('share')
    Grid() {
      ForEach($$.applicationSharings, (item: string, index) => {
        GridItem() {
          Column() {
            Image($r('app.media.startIcon'))
              .height(50)
              .width(50)
            Text(item)
              .fontSize(10)
          }
        }
      })
    }
    .height(100)
    .rowsGap(20)
    .columnsGap(20)
    .scrollBar(BarState.Off)
    .rowsTemplate('1fr')


    Grid() {
      ForEach($$.sharings, (item: string, index) => {
        GridItem() {
          Column() {
            Image($r('app.media.startIcon'))
              .height(50)
              .width(50)
            Text(item)
              .fontSize(10)
          }
        }
      })
    }
    .height(100)
    .rowsGap(20)
    .columnsGap(20)
    .scrollBar(BarState.Off)
    .rowsTemplate('1fr')


    Button('Cancel')
      .width('100%')
      .fontColor(Color.Black)
      .backgroundColor(Color.White)
      .onClick(() => {
        context.getPromptAction().closeCustomDialog(componentContent);
      })
  }
  .backgroundColor('#FFF0F0F0')
  .width('90%')
  .height('30%')
  .borderRadius(10)
}


@Entry
@Component
struct CustomShareDialog {
  @State applicationSharings: string[] =
    ['share1', 'share2', 'share3', 'share4', 'share5', 'share6', 'share7', 'share8'];
  @State sharings: string[] = ['share1', 'share2', 'share3', 'share4', 'share5', 'share6', 'share7', 'share8'];




  build() {
    Row() {
      Column() {
        Button('click me')
          .onClick(() => {
            let contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(buildText),
              new Params(this.applicationSharings, this.sharings));
            componentContent = contentNode;
            this.getUIContext().getPromptAction().openCustomDialog(contentNode);
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中如何使用自定义弹窗实现分享弹窗的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

感谢楼主分享,刚刚好需要这个

更多关于HarmonyOS鸿蒙Next中如何使用自定义弹窗实现分享弹窗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,使用自定义弹窗实现分享弹窗,首先通过@CustomDialog装饰器定义弹窗组件。在组件内构建UI布局,可包含分享平台图标和文字。通过bindContentMenu或事件绑定触发弹窗显示。使用CustomDialogController控制弹窗的显示与隐藏。在aboutToAppear中初始化数据。确保UI组件使用ArkTS声明,并遵循鸿蒙设计规范。

在HarmonyOS Next中,使用promptAction结合ComponentContent实现自定义分享弹窗是正确的方法。你的示例代码展示了核心的实现流程,这里对其关键点进行说明:

  1. 核心机制:通过UIContext.getPromptAction().openCustomDialog()来打开一个由ComponentContent封装的自定义UI组件作为弹窗。

  2. 参数传递ComponentContent构造函数允许传入一个Params对象,该对象会在@Builder函数中通过$$参数访问,这实现了弹窗内容与外部数据的动态绑定。

  3. 弹窗构建buildText这个@Builder函数定义了弹窗的完整UI,包括两个应用分享列表的Grid布局和一个关闭按钮。按钮的onClick事件通过closeCustomDialog方法关闭弹窗。

  4. 注意事项

    • 上下文获取:代码中通过AppStorage.get("context")this.getUIContext()获取UIContext。在实际开发中,需确保能正确获取到有效的上下文实例。
    • 布局适配:弹窗的宽度、高度和圆角等样式参数(如.width('90%').borderRadius(10))需要根据具体设计需求进行调整,以确保在不同设备上的显示效果。
    • 资源引用:示例中图片资源使用了$r('app.media.startIcon'),你需要确保在项目的resources目录下存在对应的图片资源。

这个实现方案提供了高度的定制灵活性,你可以根据需要修改buildText中的UI布局、样式和交互逻辑来满足不同的分享弹窗设计需求。

回到顶部