首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

仿京东短信验证码UI效果(鸿蒙)

  • 25-04-19 15:20
  • 2861
  • 8531
juejin.cn

整体思路:

外层Stack布局,里面TextInput组件用来调起键盘,Row布局中循环出四个Text组件,Row布局覆盖在TextInput组件上,用来展示输入的数字。

image.png 定义两个参数,code用来接受输入的文本,someArray用来做为想要展示的Text组件数量,其中的数字用来和code的长度做比较,来区分输入框是否写入文本。

ini
代码解读
复制代码
@State code: string = '' someArray: number[] = [1, 2, 3, 4]

这里循环someArray,Text组件未填入时显示 ‘-’并且即将写入数字的Text组件有橙色高亮,当四个输入框都有数字时,最后一个框高亮。

typescript
代码解读
复制代码
Row() { ForEach(this.someArray, (item: number, index: number) => { if (item != 1) { Blank() // 除了第一个TextView自定义组件以外,其他三个的左边都添加一个Blank()组件 } if (this.code.length >= item) { // 填过数字的输入框才满足这个条件, 且只有填到最后一个数字的时候isBorder才为true this.codeOne({ code: this.code.substring(item - 1, item), isBorder: item == 4 }) } else { // 未填数字的输入框,显示'-',并且即将要输入的框为橙色 this.codeOne({ code: '-', isBorder: this.code.length + 1 == item }) } }, (item: number) => JSON.stringify(item)) } .hitTestBehavior(HitTestMode.Transparent) .zIndex(2) .padding({ left: 16, right: 16 }) .width('100%')
scss
代码解读
复制代码
@Builder codeOne($$: SMSInfo) { Text($$.code) .textAlign(TextAlign.Center) .width(60) .height(60) .border({ radius: 11, width: 3 }) .borderColor($$.isBorder ? '#FF5500' : Color.Transparent) .backgroundColor('#F3F5F7') }

难点:需要事件透传,在Row布局上设置.hitTestBehavior(HitTestMode.Transparent),hitTestBehavior自身和子节点都响应触摸测试,不会阻塞兄弟节点的触摸测试。不会影响祖先节点的触摸测试。

完整代码:

typescript
代码解读
复制代码
import { promptAction } from "@kit.ArkUI" @Entry @Component export struct SMSCaptchaPage { @State code: string = '' someArray: number[] = [1, 2, 3, 4] build() { Stack() { Row() { ForEach(this.someArray, (item: number, index: number) => { if (item != 1) { Blank() // 除了第一个TextView自定义组件以外,其他三个的左边都添加一个Blank()组件 } if (this.code.length >= item) { // 填过数字的输入框才满足这个条件, 且只有填到最后一个数字的时候isBorder才为true this.codeOne({ code: this.code.substring(item - 1, item), isBorder: item == 4 }) } else { // 未填数字的输入框,显示'-',并且即将要输入的框为橙色 this.codeOne({ code: '-', isBorder: this.code.length + 1 == item }) } }, (item: number) => JSON.stringify(item)) } .hitTestBehavior(HitTestMode.Transparent) .zIndex(2) .padding({ left: 16, right: 16 }) .width('100%') TextInput() .width('100%') .height(50) .maxLength(4) .type(InputType.Number) .backgroundColor(Color.Transparent) .onChange((value: string) => { this.code = value // 输入的内容改变时, 实时的传给code if (value.length == 4) { promptAction.showToast({ message: '验证码输入成功,等待验证' }) } }) } .width('100%') .height('100%') } @Builder codeOne($$: SMSInfo) { Text($$.code) .textAlign(TextAlign.Center) .width(60) .height(60) .border({ radius: 11, width: 3 }) .borderColor($$.isBorder ? '#FF5500' : Color.Transparent) .backgroundColor('#F3F5F7') } } class SMSInfo { code: string = '' isBorder: boolean = false }

image.png

注:本文转载自juejin.cn的元芳想去看海的文章"https://juejin.cn/post/7473167460264722444"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

133
开发工具
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top