当前位置: 首页 > news >正文

HarmonyOS NEXT零基础入门到实战-第四部分

自定义组件:
概念: 由框架直接提供的称为 系统组件, 由开发者定义的称为 自定义组件。
源代码:

@Component
struct MyCom {
  build() {
    Column() {
      Text('我是一个自定义组件')
    }
  }
}

@Component
struct MyHeader {
  build() {
    Row() {
      Text('我是头部')
        .fontColor(Color.White)
    }
    .width('100%')
    .height(50)
    .backgroundColor(Color.Brown)
  }
}

// 快捷点 comp
@Component
struct MyMain{
  build() {
    Column() {
      MyCom()
      MyCom()
    }
    // .height(400)
    .layoutWeight(1)
    .width('100%')
    .backgroundColor(Color.Gray)
  }
}

@Component
struct MyFooter {
  build() {
    Row() {
      Text('我是底部')
        .fontColor(Color.White)
    }
    .width('100%')
    .height(50)
    .backgroundColor(Color.Green)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyHeader()
      MyMain()
      MyFooter()
    }
  }
}

自定义组件-通用属性和方法
// 尽量使用按需导出
@priview // 自定义view的预览
@Component
export struct 组件名{}

自定义组件---成员函数变量
除了必须要实现 build() 函数外,还可以定义其它的成员函数,以及成员变量
成员变量的值 -> 外部可传参覆盖  也就是带=号的变量及其方法都可以被覆盖
源代码:
@Component
struct MyPanel {
  // 成员变量
  title: string = '默认的大标题'
  extra: string = '查看更多 >'
  // 成员变量 - 函数 - 可以外部传入覆盖的
  getMore = () => {
    AlertDialog.show({
      message: '查看更多'
    })
  }

  // 成员函数 - 不可以外部传入覆盖 【没有等号】
  sayHi() {
    AlertDialog.show({
      message: '你好'
    })
  }

  build() {
    Column() {
      Row() {
        Text(this.title).fontSize(18)
        Text(this.extra).fontSize(18)
          .onClick(() => {
            this.getMore()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceBetween)
      Row() {
        Text('内容部分').fontSize(18)
      }
      .padding(20)
    }
    .width('100%')
    .height(200)
    .padding(10)
    .margin({bottom: 20})
    .borderRadius(10)
    .backgroundColor(Color.White)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyPanel({
        title: '我的订单',
        extra: '全部订单 >',
        getMore() {
          AlertDialog.show({
            message: '查看全部订单'
          })
        }
      })
      MyPanel({
        title: '小米有品众筹',
        extra: '7款众筹中 >',
        getMore() {
          AlertDialog.show({
            message: '查看7款众筹'
          })
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#ccc')
    .padding(20)
  }
}

@BuilderParam 传递UI

利用 @BuilderParam 构建函数,可以让自定义组件 允许外部传递UI。


@Component
struct SonCom {
  // 1、定义构建函数
  @BuilderParam ContentBuilder: () => void = this.defaultBuilder
  @Builder
  defaultBuilder() {
    Text('默认的内容')
      .fontColor('#000')
  }

  build() {
    // 2、使用构建函数,构建结构
    Column() {
      this.ContentBuilder()
    }
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      // SonCom()
      SonCom() {
        Button('跳转')  // 则显示这个button,否则显示上面默认的内容
      }
    }
  }
}

完善的源代码:
@Component
@Preview
struct MyPanel {
  // 成员变量
  title: string = '默认的大标题'
  extra: string = '查看更多 >'
  // 成员变量 - 函数 - 可以外部传入覆盖的
  getMore = () => {
    AlertDialog.show({
      message: '查看更多'
    })
  }

  // 成员函数 - 不可以外部传入覆盖 【没有等号】
  sayHi() {
    AlertDialog.show({
      message: '你好'
    })
  }

  @BuilderParam ContentBuilder: () => void = this.defaultBuilder
  @Builder
  defaultBuilder() {Text('默认文本')}

  build() {
    Column() {
      Row() {
        Text(this.title).fontSize(18)
        Text(this.extra).fontSize(18)
          .onClick(() => {
            this.getMore()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceBetween)
      Row() {
        // Text('内容部分').fontSize(18)
        // 这里的结构不希望写死,需要通过 BuilderParams 来进行构建
        this.ContentBuilder()
      }
      .padding(20)
    }
    .width('100%')
    .height(200)
    .padding(10)
    .margin({bottom: 20})
    .borderRadius(10)
    .backgroundColor(Color.White)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyPanel({
        title: '我的订单',
        extra: '全部订单 >',
        getMore() {
          AlertDialog.show({
            message: '查看全部订单'
          })
        }
      }) {
        Text('我是订单 - 相关的文本')
      }
      MyPanel({
        title: '小米有品众筹',
        extra: '7款众筹中 >',
        getMore() {
          AlertDialog.show({
            message: '查看7款众筹'
          })
        }
      }) {
        Button('我是小米有品众筹的按钮')
      }
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#ccc')
    .padding(20)
  }
}

效果图:

多个 @BuilderParam 参数
子组件有多个BuilderParam, 必须通过参数的方式来传入

@Component
@Preview
struct MyCard {@BuilderParam titleBuilder: () => void = this.titleDefaultBuilder@BuildertitleDefaultBuilder() {Text('我是默认的大标题')}@BuilderParam contentBuilder: () => void = this.contentDefaultBuilder@BuildercontentDefaultBuilder() {Text('我是默认的内容')}build() {// 卡片组件Column() {// 标题部分Row() {this.titleBuilder()}.height(30).width('100%').border({color: '#ccc', width: {bottom: 1}}).padding({left: 10})// 内容部分Row() {this.contentBuilder()}.width('100%').padding(10)}.width('100%').height(100).borderRadius(10).backgroundColor(Color.White).justifyContent(FlexAlign.Start)}
}@Entry
@Component
struct Index {@Builder ftBuilder() {Text('我是传入的大标题结构')}@Builder fcBuilder() {Text('我是传入的大标题结构')}build() {Column({space: 10}) {// 希望标题部分和内容部分是自定义的MyCard({titleBuilder: this.ftBuilder,contentBuilder: this.fcBuilder})MyCard({titleBuilder: this.ftBuilder,contentBuilder: this.fcBuilder}) // {} 尾随闭包是不可以}.width('100%').height('100%').padding(20).backgroundColor('#ccc')}
}


 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 速盾:cdn能防御ddos吗?
  • Codeforces Round 874 (Div. 3)(A~D题)
  • 掌握AJAX技术:从基础到实战
  • reduceByKey 函数详解
  • 1-如何挑选Android编译服务器
  • Git拉取国外远程嵌套代码
  • Kylin自定义函数全解:释放数据分析的无限潜能
  • 【Web】LitCTF 2024 题解(全)
  • JavaScript数据筛选和模糊搜索
  • Infuse Pro for Mac全能视频播放器
  • PySide(PyQt)的QPropertyAnimation(属性动画)的应用实践
  • vue elementui 在table里使用el-switch
  • 经典文献阅读之--LIV-GaussMap(实时3D辐射场地图渲染的LiDAR惯性视觉融合算法)
  • tmux相关命令
  • 2024年7月25日(Git gitlab以及分支管理 )
  • 自己简单写的 事件订阅机制
  • CSS盒模型深入
  • ECS应用管理最佳实践
  • gitlab-ci配置详解(一)
  • java多线程
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • Puppeteer:浏览器控制器
  • Python爬虫--- 1.3 BS4库的解析器
  • 从伪并行的 Python 多线程说起
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 人脸识别最新开发经验demo
  • 项目管理碎碎念系列之一:干系人管理
  • 一、python与pycharm的安装
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 在Mac OS X上安装 Ruby运行环境
  • 正则学习笔记
  • 自动记录MySQL慢查询快照脚本
  • 树莓派用上kodexplorer也能玩成私有网盘
  • 我们雇佣了一只大猴子...
  • ​渐进式Web应用PWA的未来
  • # 利刃出鞘_Tomcat 核心原理解析(八)-- Tomcat 集群
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • (1)(1.9) MSP (version 4.2)
  • (30)数组元素和与数字和的绝对差
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (二刷)代码随想录第15天|层序遍历 226.翻转二叉树 101.对称二叉树2
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (一)WLAN定义和基本架构转
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (一)十分简易快速 自己训练样本 opencv级联haar分类器 车牌识别
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)3D模板阴影原理
  • (转)linux下的时间函数使用
  • (转)编辑寄语:因为爱心,所以美丽
  • ***监测系统的构建(chkrootkit )