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

样式复用多态

@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

http://www.cnnetsun.cn/news/136845.html

相关文章:

  • 关于Comtos Linux (朱雀)主体源码的选择
  • 超级Mini小车功能说明
  • STC32G12单片机替换成STC32F12单片机,直接替换的结果
  • SIEMENS 6SL3210-1PE33-0CL0 变频器
  • 软件测试常用的7种方法,最后一个是升职加薪关键!(零基础小白转行IT互联网高效进阶)
  • 【RTOS】EasyLog的移植与使用
  • 在数据库里玩“平行宇宙”:MatrixOne Data Branch 让数据也拥有Git 的分支/合并/对比/回滚(含跨集群同步)
  • 基于单片机的全自动洗衣机系统的设计
  • 5.6 模型部署与智能体集成实战
  • 基于单片机的球赛计分牌的设计
  • ArcGIS Pro 从入门到实战基础篇(10):地图菜单
  • Kotaemon与Redis/Memcached集成:构建高速缓存层
  • 【鸿蒙三方库编译】lycium_plusplus(lycium++)高效完成鸿蒙C/C++编译
  • 2025年度GEO服务商权威甄选指南:技术深度与商业价值的双重考量
  • 收藏备用!Java程序员转AI大模型:从技术沉淀到AI爆发的进阶之路
  • Python 爬虫实战:Session 会话维持爬取需登录内容
  • 基于移相全桥变换器的电池充电仿真模型,采用电压电流双闭环PI控制。 电池先经历CC模式而后进入...
  • 基于COMSOL模拟的水力压裂技术研究:固体力学与达西定理的应用
  • Redis 性能调优(二)
  • Doris 性能调优实践指南(可直接落地)
  • presum|二分try+滑窗cnt
  • Web自动化测试:Unittest单元测试框架
  • Apache2最佳实践
  • 实力派,也可以是偶像派
  • 基于单片机的多功能万年历
  • AI搜索时代:技术演进、产业分化与深度变革
  • SGMICRO圣邦微 SGM2019-2.5YC5G/TR SC70-5 线性稳压器(LDO)
  • 一文搞懂 低功耗蓝牙BLE 中的 ATT、GATT、MTU 与 20 字节限制
  • 别让“大锅饭”逼走你的Top Sales:揭秘薪酬误差的副作用
  • 27827828