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

4.ArkUI TextInput的介绍和使用

ArkUI TextInput 组件详解与使用指南

TextInput 是 ArkUI 中用于文本输入的核心组件,支持单行、多行输入及多种输入格式控制。以下是 TextInput 的详细使用方法及示例:


基础使用

1. 基本输入框

@Entry
@Component
struct BasicInputExample {@State text: string = ''build() {Column() {TextInput({ placeholder: '请输入内容' }).width('80%').height(40).onChange((value: string) => {this.text = value})Text('输入内容: ' + this.text).margin({ top: 10 })}.width('100%').height('100%').justifyContent(FlexAlign.Center)}
}

核心功能

1. 输入类型控制

TextInput().type(InputType.Normal)      // 常规文本(默认).type(InputType.Password)    // 密码输入.type(InputType.Email)       // 邮箱格式.type(InputType.Number)      // 数字键盘.type(InputType.PhoneNumber) // 电话号码

2. 输入限制

TextInput().maxLength(10)             // 最大输入长度.maxLines(3)               // 最大行数(多行模式).inputFilter('[a-zA-Z]')   // 正则过滤(只允许字母)

3. 样式定制

TextInput().fontSize(16)              // 字体大小.fontColor('#333')         // 字体颜色.placeholderColor('#999')  // 提示文字颜色.backgroundColor('#FFF')   // 背景颜色.border({                  // 边框样式width: 1,color: '#DDD',radius: 8}).padding(10)               // 内边距

高级功能

1. 多行输入

@State multiText: string = ''build() {TextInput({ text: this.multiText }).width('90%').height(120).type(InputType.MultiLine) // 启用多行模式.onChange((value: string) => {this.multiText = value})
}

2. 焦点控制

private controller: TextInputController = new TextInputController()build() {Column() {TextInput({ controller: this.controller }).onClick(() => {this.controller.caretPosition(5) // 设置光标位置})Button('获取焦点').onClick(() => {this.controller.focus() // 手动聚焦})Button('失去焦点').onClick(() => {this.controller.blur() // 取消聚焦})}
}

3. 输入验证

@State email: string = ''
@State errorMsg: string = ''validateEmail(email: string) {const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/this.errorMsg = pattern.test(email) ? '' : '邮箱格式不正确'
}build() {Column() {TextInput({ placeholder: '输入邮箱' }).type(InputType.Email).onChange((value: string) => {this.email = valuethis.validateEmail(value)})Text(this.errorMsg).fontSize(12).fontColor(Color.Red)}
}

最佳实践

1. 搜索框实现

@Entry
@Component
struct SearchExample {@State keyword: string = ''@State searchResults: string[] = []build() {Column() {// 搜索框TextInput({ placeholder: '搜索内容...' }).width('90%').height(40).icon(Image($r('app.media.ic_search'))) // 搜索图标.onSubmit((value: string) => {         // 提交事件(点击键盘确认)this.performSearch(value)}).onChange((value: string) => {this.keyword = value})// 搜索结果列表List() {ForEach(this.searchResults, (item) => {ListItem() {Text(item)}})}}}performSearch(keyword: string) {// 模拟搜索逻辑this.searchResults = ['结果1', '结果2', '结果3']}
}

2. 密码可见性切换

@Entry
@Component
struct PasswordInput {@State password: string = ''@State showPassword: boolean = falsebuild() {Row() {TextInput({ placeholder: '输入密码',text: this.password }).type(this.showPassword ? InputType.Normal : InputType.Password).layoutWeight(1)Button(this.showPassword ? '👁️' : '👁️🗨️').onClick(() => {this.showPassword = !this.showPassword})}.width('80%').padding(10).border({ width: 1, color: '#DDD' }).borderRadius(8)}
}

注意事项

  1. 性能优化

    • 避免在 onChange 中执行耗时操作
    • 大数据量实时搜索使用防抖(300-500ms)
  2. 无障碍支持

    TextInput().accessibilityLabel('用户名输入框').accessibilityHint('请输入您的用户名')
    
  3. 键盘控制

    TextInput().enterKeyType(EnterKeyType.Go)       // 键盘确认键类型.onSubmit((value: string) => {       // 确认键回调// 处理提交逻辑})
    
  4. 多语言支持

    TextInput({ placeholder: $r('app.string.enter_username') })
    

通过合理使用 TextInput 组件,可以实现从简单到复杂的各种文本输入场景。关键点在于:

  • 根据场景选择合适的输入类型
  • 结合状态管理实现数据绑定
  • 通过样式定制提升用户体验
  • 添加必要的输入验证和安全措施
http://www.xdnf.cn/news/115057.html

相关文章:

  • 多语言虚拟币海外游戏娱乐平台源码详解(整合篇)
  • 上岸率85%+,25西电先进材料与纳米科技学院(考研录取情况)
  • 【投屏软件】手机投屏软件
  • 省时省力的AI批量原创SEO文章生成工具解放双手
  • CentOS 7 基于 Nginx 的 HTML 部署全流程指南
  • 18.应用聚合、指标显示、应用状态,从Heimdall说起(二)
  • 十分钟恢复服务器攻击——群联AI云防护系统实战
  • 【专题刷题】二分查找(一):深度解刨二分思想和二分模板
  • PostgreSQL 数据库备份与恢复全面指南20250424
  • 反爬系列 IP 限制与频率封禁应对指南
  • DBdriver使用taos数据库
  • 神经网络基础[ANN网络的搭建]
  • 【晶振】晶振的工作原理及其与单片机关系
  • CGAL 网格内部生成随机点
  • 扩展中国剩余定理
  • 高企复审奖补!2025年合肥市高新技术企业重新认定奖励补贴政策及申报条件
  • 项目右键没有add as maven project选项
  • 栈(Stack)和队列(Queue)
  • 华为手机怎么进行音频降噪?音频降噪技巧分享:提升听觉体验
  • 【前端】【业务场景】【面试】在前端开发中,如何实现一个可拖动和可缩放的元素,并且处理好边界限制和性能优化?
  • PS Mac Photoshop 2025 for Mac图像处理 PS 2025安装笔记
  • SQL Server 2008 R2中varchar(max)的含义
  • 如何获取静态IP地址?完整教程
  • ESP32上C语言实现JSON对象的创建和解析
  • 亚马逊英国站FBA费用重构:轻小商品迎红利期,跨境卖家如何抢占先机?
  • 动态渲染页面智能嗅探:机器学习判定AJAX加载触发条件
  • Visual Studio Code 使用tab键往左和往右缩进内容
  • 差分信号抗噪声原理:
  • 编译 C++ 报错“找不到 g++ 编译器”的终极解决方案(含 Windows/Linux/macOS)
  • MacOS上如何运行内网穿透详细教程