「HarmonyOS」CustomDialogController自定义弹窗使用方法

本文介绍了如何在ArkTs开发中使用CustomDialogController创建自定义弹窗,包括样式设置、实践操作步骤以及如何在修改个性签名等场景中调用和关闭弹窗。同时提及了HarmonyOs的相关背景和资源管理。
摘要由CSDN通过智能技术生成
需求背景:

在开发的过程中,总会遇到一些功能需要使用到弹窗进行信息的输入和修改,如用户个人信息的修改;在UI设计上每个App通常都会有各自的样式,而不是使用系统的标准样式,所以通常我们需要进行自定义弹窗来实现信息填写需求

模块介绍

在ArkTs中,CustomDialogController提供这个对应功能,如下是在官方文档中的介绍:

通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。

样式展示

先进行样式展示一下,然后根据这个样式进行代码编写
在这里插入图片描述

这是当输入有误时的样式,当输入超过限制字符数量时,会展示错误提示,并且不能继续添加输入,当删除后字数小于限制字数时,错误提示消失
在这里插入图片描述

实践操作

该自定义弹窗需要复用,需要用时给于修改昵称和修改个性签名使用,所以在标题,输入框提示语等内容上会有所不同

1.创建自定义弹窗样式

需要使用@CustomDialog装饰器来表达这是一个自定义弹窗

@CustomDialog
struct UserInputDialogView {
  // 弹窗标题
  @State dialogViewTitle: string = ''
  // 输入框提示语
  @State placeHolderContent: string = ''
  // 是否展示错误提示
  @State isShowErrorMsg: boolean = false
  // 是否超过了字数限制
  @State MoreMaxLength: boolean = false
  // 输入的内容
  @State textValue: string = ''
  // 最大输入字符数字
  inputMaxNumber: number
  controller: CustomDialogController
  // 取消方法回调
  cancel: () => void
  // 确认方法回调
  confirm: (text: string) => void

  build() {
    Column() {
      Row() {
        Text(this.dialogViewTitle)
          .fontSize(16)
          .fontColor($r('app.color.24292B'))
          .fontWeight(FontWeight.Medium)
      }
      .justifyContent(FlexAlign.Center)
      .alignItems(VerticalAlign.Center)
      .width(BaseUtils.screenWidth - 38)
      .height(24)
      .margin({top: 16})
      
      // 直接引用输入框提示内容和输入内容
      TextInput({placeholder: this.placeHolderContent, text: this.textValue})
        .width(BaseUtils.screenWidth - 64)
        .height(56)
        .backgroundColor($r('app.color.F5F5F5'))
        .caretColor($r('app.color.FF8000'))
        .borderRadius(8)
        .margin({top: 16})
        .type(InputType.Normal)
        // 用于打开弹窗后,焦点直接落在弹窗上,以便于直接弹出输入键盘
        .key('popUpKeyboard')
        .onFocus(() => {
          sendEventByKey('popUpKeyboard', 10, '弹出键盘')
        })
        .onChange((value) => {
          this.textValue = value
          if (value.length > this.inputMaxNumber) {
          	// 如果输入内容超过限制字符,展示错误提示
            this.MoreMaxLength = true
            this.textValue = this.textValue.substring(0,this.inputMaxNumber)

          } else if (value.length == this.inputMaxNumber) {
           	// 如果输入内容长度等于限制字符
           	// 如果为刚好等于则不展示
           	// 如果已超出限制字符则展示错误提示
            if (this.MoreMaxLength) {
              this.isShowErrorMsg = true
            }

          } else {
            this.isShowErrorMsg = false
            this.MoreMaxLength = false
          }
        })

	  // 错误提示
      if (this.isShowErrorMsg) {
        Text(`最多不超过${this.inputMaxNumber}个字`)
          .fontSize(12)
          .fontColor($r('app.color.F7313B'))
          .fontWeight(FontWeight.Regular)
          .textAlign(TextAlign.Start)
          .width(BaseUtils.screenWidth - 72)
          .margin({top: 2})
      }

      Flex({direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween}) {
        Button('取消')
          .fontSize(16)
          .fontColor($r('app.color.24292B'))
          .fontWeight(FontWeight.Medium)
          .backgroundColor($r('app.color.F7F7F7'))
          .size({width: 144, height: 44})
          .borderRadius(22)
          .onClick(() => {
            this.controller.close()
            this.cancel()
          })

        Button('确定')
          .fontSize(16)
          .fontColor(this.textValue.length == 0 ? $r('app.color.FFFFFF_50') : $r('app.color.FFFFFF'))
          .fontWeight(FontWeight.Medium)
          .backgroundColor(this.textValue.length == 0 ? $r('app.color.FF8000_50') : $r('app.color.FF8000'))
          .size({width: 144, height: 44})
          .borderRadius(22)
          .enabled(this.textValue.length == 0 ? false : true)
          .margin({left: 25})
          .onClick(() => {
          	// 确认按钮 返回输入文本内容,并且关闭自定义弹窗
            this.confirm(this.textValue)
            this.controller.close()
          })
      }
      .width(BaseUtils.screenWidth - 64)
      .height(44)
      .margin({top: this.isShowErrorMsg ? 8 : 24})
    }
    .alignItems(HorizontalAlign.Center)
    .width(BaseUtils.screenWidth)
    .backgroundColor($r('app.color.FFFFFF'))
    .height(215)
    .borderRadius(16)
  }
}
2.初始化自定义弹窗

以修改个性签名弹窗为例

userSignDialogController: CustomDialogController = new CustomDialogController({
    builder: UserInputDialogView({
      dialogViewTitle: '个性签名',
      placeHolderContent: '请输入个性签名(限制15个字)',
      inputMaxNumber: 15,
      cancel: () => {},
      confirm: (text: string) => {
      	// 修改个性签名请求 
        HttpApiManager.getInstance().updateUserInfo(new UserInfoBean(null, null, text))
          .then((data: UserInfoBean) => {
            this.userInfo = data
            // 用户数据更新通知
            // 具体可见我《EventHub事件通知详细使用方法》文章
            EventHubUtil.emit('updateUserInfo')
            ToastUtil.getInstance().showToast('修改成功')

          })
          .catch(error => {
            ToastUtil.getInstance().showToast(error.message)
          })
      },
    }),
    // 是否点击弹窗其他地方蒙层关闭
    autoCancel: true,
    // 弹窗在竖直方向上的对齐方式
    alignment: DialogAlignment.Default,
    // 是否使用自定义样式
    customStyle: true
  })
3.销毁自定义弹窗

在页面销毁前,需销毁自定义弹窗,以避免系统资源浪费

aboutToDisappear() {
    delete this.userSignDialogController,
    this.userSignDialogController = undefined
  }
4.调用/打开自定义弹窗

在摁钮或者View的onClick方法中进行调用方法以实现点击打开

if (this.userSignDialogController != undefined) {
    this.userSignDialogController.open()
}
5.关闭自定义弹窗

这个在自定义弹窗的取消和确认按钮中有写入该方法

//  取消按钮的点击事件
.onClick(() => {
   // 关闭自定义弹窗
   this.controller.close()
   this.cancel()
})
参考文档

自定义弹窗API
自定义弹窗(CustomDialog)指南

当前HarmonyOs仍在初步学习过程中,大家如果感兴趣或者有问题可以一起沟通交流
如果该文章对你有所帮助的话,可以点赞、收藏并关注一下!后续会持续更新更多技术内容

要在HarmonyOS使用TCP通信,您需要使用套接字(socket)API。以下是使用套接字API进行TCP通信的基本步骤: 1. 创建套接字:使用socket()函数创建一个套接字。指定套接字类型为SOCK_STREAM,表示使用TCP协议进行通信。 2. 绑定套接字:使用bind()函数将套接字绑定到本地IP地址和端口号。 3. 连接服务器:使用connect()函数连接到服务器IP地址和端口号。 4. 发送数据:使用send()函数发送数据到服务器端。 5. 接收数据:使用recv()函数从服务器端接收数据。 6. 关闭套接字:使用close()函数关闭套接字。 下面是一个简单的使用socket API进行TCP通信的示例: ```c #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); // 创建套接字 struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); // 绑定本地IP地址和端口号 bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)); struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); inet_pton(AF_INET, "192.168.1.1", &server_addr.sin_addr); // 连接服务器 connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); char buf[1024]; strcpy(buf, "Hello, World!"); send(sockfd, buf, strlen(buf), 0); // 发送数据 recv(sockfd, buf, sizeof(buf), 0); // 接收数据 printf("Received: %s\n", buf); close(sockfd); // 关闭套接字 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡酒交魂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
>