鸿蒙NEXT版实战开发:UI框架-如何使用自定义弹窗实现分享弹窗?

往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)


如何使用自定义弹窗实现分享弹窗

可以使用promptAction结合ComponentContent实现自定义分享弹窗,示例代码如下:

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

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

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

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

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

    Button('取消')
      .width('100%')
      .fontColor(Color.Black)
      .backgroundColor(Color.White)
      .onClick(() => {
        promptAction?.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 uiContext = this.getUIContext();
            promptAction = uiContext.getPromptAction();
            let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText),
              new Params(this.applicationSharings, this.sharings));
            componentContent = contentNode;
            promptAction.openCustomDialog(contentNode);
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值