news 2026/4/29 8:10:05

样式复用多态

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
样式复用多态

@Styles

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-style-0000001473856690-V2

如果每个组件的样式都需要单独设置,在开发过程中会出现大量代码在进行重复样式设置,虽然可以复制粘贴,但为了代码简洁性和后续方便维护,我们推出了可以提炼公共样式进行复用的装饰器@Styles。

@Styles装饰器可以将多条样式设置提炼成一个方法,直接在组件声明的位置调用。通过@Styles装饰器可以快速定义并复用自定义样式。用于快速定义并复用自定义样式

  • 局部定义

// 全局 @Styles function functionNameStyle() { .width(150) .height(100) .backgroundColor(Color.Red) } ​ @Entry @Component sturt Index { // 组件内 @Styles functionNameStyle() { ... } ​ build() { Text('Text') .functionNameStyle() } }
  • demo

@Entry @Component struct Index { ​ @Styles iptStyle() { .width('80%') .height(50) .backgroundColor(Color.White) .border({ width: 1, color: Color.Gray }) .margin({top:20,bottom: 20}) } ​ build() { Column() { TextInput({placeholder:'请输入手机号'}) .iptStyle() ​ TextInput({placeholder:'请输入验证码'}) .iptStyle() } } }
  • 案例

// function 函数名() {} // @Styles function 函数名Style() {} // @Styles function iptStyle() { // .width('90%').height(50).backgroundColor('#fff') // .border({ // width:{bottom:2}, // color: '#e5e5e5' // }) // .margin({top:20}) // .borderRadius(0) // } ​ @Entry @Component struct Index { ​ @Styles iptStyle() { .width('90%').height(50).backgroundColor('#fff') .border({ width:{bottom:2}, color: '#e5e5e5' }) .margin({top:20}) .borderRadius(0) } ​ build() { Column(){ TextInput({placeholder:'请输入手机号'}).iptStyle() TextInput({placeholder:'请输入验证码'}).iptStyle() } } }

@Extend

@Extend用于扩展原生组件样式,通过传参提供更灵活的样式复用

  • 仅支持全局

  • 支持传参,传递状态自动同步,也可以传递回调函数

// 全局 原生组件 参数 // ↓ ↓ ↓ @Extend(Text) function 属性名字(data: number) { .width(data) }
  • demo1

// @Styles function iptStyle() { @Extend(TextInput) function iptStyle(width:number=80) { // .width('80%') .width(`${width}%`) .backgroundColor('#fff') .border({ width: 1, color: '#ccc', style: BorderStyle.Solid }) .margin({top:20,bottom:20}) } ​ @Entry @Component struct Index { build() { Column() { Text('登录页') ​ TextInput({placeholder:'请输入用户名'}) .iptStyle() // .width('100%') 不推荐, 后者覆盖前者 ​ Row() { TextInput({placeholder:'验证码'}) .iptStyle(40) Button('获取验证码') } ​ } } }
  • demo2

@Extend(TextInput) function iptStyle(cb: (data:string) => void,w:number=80) { .width(`${w}%`) .height(50) .backgroundColor(Color.White) .border({ width: 1, color: Color.Gray }) .margin({top:20,bottom: 20}) .onChange(cb) } ​ @Entry @Component struct Index { @State mobile:string = '' @State code:string = '' ​ @State w:number = 40 build() { Column() { Text(`手机号:${this.mobile}`) Text(`验证码:${this.code}`) ​ TextInput({placeholder:'请输入手机号'}) .iptStyle((data:string) => { this.mobile = data }) ​ TextInput({placeholder:'请输入验证码'}) .iptStyle((data:string) => { this.code = data },40) ​ TextInput({placeholder:'点击变成'}) .iptStyle((data:string) => { this.code = data },this.w) .onClick(() =>{ this.w=90 }) .onBlur(() => { this.w =40 }) } } }

多态

stateStyles()可以依据组件的内部状态的不同,快速设置不同样式。

  • normal:正常态。

  • pressed:按压态。

  • focused:获焦态。 DevEco5 有bug@24.6

  • disabled:不可用态。

@Entry @Component struct Index { ​ @State disabled:boolean = false // true 禁用, false没有禁用 启用 ​ build() { Column() { Text('通过Text内置组件模拟实现Button效果').fontSize(20) ​ // TextInput({placeholder:'hello'}) // .stateStyles({ // input打开后默认第一个自动获取焦点 // focused: { // .backgroundColor('red') // } // }) // // TextInput({placeholder:'hello'}) // .stateStyles({ // 第二个得自己点 // focused: { // .backgroundColor('red') // } // }) ​ Text('删除') .width(100).height(50) .fontColor('#fff') // .backgroundColor('#000') .borderRadius(25) .textAlign(TextAlign.Center) .focusable(true) // 获取焦点,切记页面不能有默认获取焦点组件否相互影响 // .enabled(false) // 是否激活状态: true-启动,false禁用 应该灰色 这哥们比较矫情需要点击修改状态才可以禁用 类似于获取短信验证码 不是立马禁用而是操作后 .enabled(!this.disabled) .stateStyles({ normal: { .backgroundColor('red') }, pressed: { .backgroundColor('#000') }, // focused: { // .backgroundColor('blue') // }, disabled: { .backgroundColor('#ccc') } }) ​ ​ Text(`当前状态:${this.disabled}-${this.disabled?'已禁用':'正常'}`) // disabled为真代表禁用了, 咱们点击按钮让他启用 Button(this.disabled?'启用':'禁用').onClick(() => this.disabled = !this.disabled) } .padding(30) } }
  • 获取短信

@Entry @Component struct Index { // 准备 完成布局 包括禁用效果 等等之类的 // 1 声明响应式数据 // content=获取验证码、time=60 学习咱们用10、timer定时器 后期清楚、 disabled 是否禁用 // 并且视图使用 // 2 绑定点击事件 // 4 事件处理函数中 // - 4.1 过滤 手机号过滤、避免重复点击 // - 4.2 立马-1 并同步页面 // - 4.3 每隔1秒-1 并且判断 time<=1 =》先清空定时器,接着响应式数据还原,最后终止代码执行 // - 4.4 发送服务器请求 让服务器发送短信验证码 ​ @State content:string = '获取验证码' @State isEnabled:boolean = true ​ private time:number = 10 private timer:number = 0 ​ build() { Text(this.content) .fontSize(40) .fontColor(this.isEnabled?Color.White:'#ccc') // .fontColor('#ccc') .borderRadius(30) .padding(20) // 是否可交互 true-可以 没有禁用, false-不可以 也就禁用了 .enabled(this.isEnabled) .stateStyles({ normal: { .backgroundColor(Color.Black) }, disabled: { .backgroundColor(Color.Gray) }, }) .onClick(() => { // - 4.1 过滤 手机号过滤、避免重复点击 if (!this.isEnabled) return // - 4.2 立马-1 并同步页面 this.isEnabled = false this.time-- this.content = `剩余${this.time}s` // - 4.3 每隔1秒-1 并且判断 time<=1 =》先清空定时器,接着响应式数据还原,最后终止代码执行 this.timer = setInterval(() => { // 返回number 第一次1 第二次2 唯一的 if (this.time<=1) { clearInterval(this.timer) this.content = '获取验证码' this.isEnabled = true this.time = 10 this.timer = 0 return } this.time-- this.content = `剩余${this.time}s` }, 1000) // - 4.4 发送服务器请求 让服务器发送短信验证码 console.log('请求接口') }) } }

欢迎加入课程班级,考取鸿蒙认证:

https://developer.huawei.com/consumer/cn/training/classDetail/d43582bb30b34f548c16c127cb3be104?type=1?ha_source=hmosclass&ha_sourceId=89000248

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 11:24:27

【AI金融】手搓巴菲特智能体:大模型投资助手开发全流程!

简介 本文详细介绍了如何利用大模型技术创建"巴菲特风格"投资智能体的实战过程&#xff0c;包括Multi-Agent系统架构设计、ADP平台实现及工作流编排。作者分享了从创意构思到落地开发的完整流程&#xff0c;重点解决了Agent幻觉、网页生成失败等技术难题&#xff0c;…

作者头像 李华
网站建设 2026/4/25 16:10:57

2023A卷,基站维修工程师

👨‍⚕️ 主页: gis分享者 👨‍⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅! 👨‍⚕️ 收录于专栏:华为OD面试 文章目录 一、🍀前言 1.1 ☘️题目详情 1.2 ☘️参考解题答案 一、🍀前言 2023A卷,基站维修工程师。 1.1 ☘️题目详情 题目: 小王是…

作者头像 李华
网站建设 2026/4/25 23:58:42

STC32G144K逐飞开源库发布!

简 介&#xff1a; 本文介绍了STC32G144K246高性能微控制器及其开源库在智能车竞赛中的应用。该芯片相比前代STC32G12K显著提升了硬件资源&#xff0c;包括6组16位PWM定时器、14个24位定时器和8组USART串口&#xff0c;解决了资源瓶颈问题。开源库优化了延时精度、串口灵活性和…

作者头像 李华
网站建设 2026/4/17 17:47:48

基于单片机的高精度超声波超声波测距系统实现

第一章&#xff1a;系统设计目标与需求分析 本系统旨在通过超声波技术实现非接触式距离精确测量&#xff0c;解决传统测距方法&#xff08;如红外测距&#xff09;精度低、受环境光干扰大的问题&#xff0c;适用于机器人避障、液位检测、工业位移监测等场景。核心需求包括&…

作者头像 李华
网站建设 2026/4/19 23:42:05

基于单片机的防盗报警器系统设计

第一章&#xff1a;系统设计目标与需求分析 基于单片机的防盗报警器系统旨在通过智能化的入侵检测与快速响应机制&#xff0c;解决传统安防设备误报率高、联动性差的问题&#xff0c;适用于家庭、商铺等小型场所的安全防护。核心需求包括&#xff1a;精准检测人体入侵&#xff…

作者头像 李华