news 2026/4/10 15:53:23

样式复用多态

作者头像

张小明

前端开发工程师

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/3/25 12:42:50

管理案例丨华恒智信助力某电力设计公司绩效考核体系升级项目——以“角色定位”与“多维评价”模型,破解国企业务升级下的价值衡量难题

【客户行业】电力行业、电力工程设计咨询、工程总承包&#xff08;EPC&#xff09;、基础设施建设领域 【问题类型】绩效考核体系优化、转型期组织绩效管理、项目管理与经营类岗位价值评价一、客户背景&#xff1a;从“设计院”到“工程公司”转型中的管理挑战某电力设计公司成…

作者头像 李华
网站建设 2026/3/31 6:06:52

AgentWeb升级实战手册:从架构重构到性能飞跃

AgentWeb升级实战手册&#xff1a;从架构重构到性能飞跃 【免费下载链接】AgentWeb AgentWeb is a powerful library based on Android WebView. 项目地址: https://gitcode.com/gh_mirrors/ag/AgentWeb 还记得那个让你夜不能寐的WebView升级噩梦吗&#xff1f;页面白屏…

作者头像 李华
网站建设 2026/4/3 6:46:19

基于单片机的红外测温系统设计

第一章&#xff1a;系统设计目标与需求分析 本系统旨在通过红外技术实现非接触式温度快速测量&#xff0c;解决传统测温需接触、响应慢的问题&#xff0c;适用于人体体温筛查、设备温升监测等场景。核心需求包括&#xff1a;测温范围设定为-50℃380℃&#xff0c;覆盖多数日常与…

作者头像 李华
网站建设 2026/4/4 10:17:35

使用Postman之上一个接口的返回值作为下一个接口的入参

在使用Postman做接口测试的时候&#xff0c;在多个接口的测试中&#xff0c;如果需要上一个接口的返回值作为下一个接口的入参&#xff0c;其基本思路是&#xff1a; 1、获取上一个接口的返回值 2、将返回值设置成环境变量或者全局变量 3、设置下一个接口的参数形式 下面我…

作者头像 李华
网站建设 2026/4/3 20:30:21

4.6 你的 Agent 安全吗?Prompt 注入、数据泄露攻击模式与防御策略

4.6 你的 Agent 安全吗?Prompt 注入、数据泄露攻击模式与防御策略 导语:在上一章,我们构建了一个“AI 防火墙”,学会了如何检测正在发生的攻击。然而,在安全领域,仅仅满足于“发现问题”是远远不够的。一个真正健壮的系统,需要构建**纵深防御(Defense in Depth)**体系…

作者头像 李华