
https://www.bilibili.com/video/BV1jomdBBE4H/

TextInput 是控件库中的基础文本输入框组件,支持验证、提示、图标、多种尺寸和状态,适用于表单输入、搜索、数据录入等场景。
文本输入框采用清晰易用设计,具有以下特点:
import { TextInput } from '../components/base'
@Entry
@Component
struct MyPage {
@State inputValue: string = ''
build() {
Column({ space: 20 }) {
// 基础输入框
TextInput({
value: $inputValue,
placeholder: '请输入内容'
})
// 带标签的输入框
TextInput({
value: $inputValue,
placeholder: '请输入用户名',
label: '用户名'
})
// 带提示的输入框
TextInput({
value: $inputValue,
placeholder: '请输入邮箱',
label: '邮箱',
hint: '请输入有效的邮箱地址'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}TextInput 使用 @Link 实现双向绑定,需要使用 $variableName 语法:
@State inputValue: string = ''
TextInput({
value: $inputValue // 使用 $ 创建双向绑定
})属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | @Link string | - | 输入框值(必需,双向绑定) |
placeholder | string | '请输入' | 占位符文本 |
label | string? | undefined | 标签文本(可选) |
hint | string? | undefined | 提示文本(可选,显示在输入框下方) |
errorMessage | string? | undefined | 错误提示文本(可选,优先级高于 hint) |
inputSize | 'small' | 'medium' | 'large' | 'medium' | 输入框尺寸 |
disabled | boolean | false | 是否禁用 |
readonly | boolean | false | 是否只读 |
required | boolean | false | 是否必填 |
maxLength | number? | undefined | 最大长度 |
type | 'text' | 'email' | 'number' | 'password' | 'tel' | 'url' | 'text' | 输入类型 |
leftIcon | ResourceStr? | undefined | 左侧图片图标(可选) |
leftTextIcon | string? | undefined | 左侧文字图标(可选,优先级高于 leftIcon) |
rightIcon | ResourceStr? | undefined | 右侧图片图标(可选) |
rightTextIcon | string? | undefined | 右侧文字图标(可选,优先级高于 rightIcon) |
showClearButton | boolean | true | 是否显示清除按钮 |
showBrand | boolean | true | 是否显示品牌标识 |
inputWidth | string | number? | undefined | 输入框宽度 |
validator | @BuilderParam (value: string) => ValidationResult? | undefined | 验证函数(可选) |
onChangeBuilder | @BuilderParam (value: string) => void? | undefined | 值变化回调(可选) |
属性名 | 类型 | 说明 |
|---|---|---|
valid | boolean | 验证是否通过 |
message | string? | 验证消息(可选) |
尺寸 | 高度 | 字体大小 | 图标大小 | 内边距(左右) |
|---|---|---|---|---|
small | 32vp | 12vp | 16vp | 12vp |
medium | 40vp | 14vp | 18vp | 14vp |
large | 48vp | 16vp | 20vp | 16vp |
@Entry
@Component
struct InputExample1 {
@State inputValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $inputValue,
placeholder: '请输入内容'
})
Text(`当前值:${this.inputValue || '(空)'}`)
.fontSize(14)
.fontColor('#666666')
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample2 {
@State inputValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $inputValue,
placeholder: '小尺寸',
inputSize: 'small'
})
TextInput({
value: $inputValue,
placeholder: '中等尺寸(默认)',
inputSize: 'medium'
})
TextInput({
value: $inputValue,
placeholder: '大尺寸',
inputSize: 'large'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample3 {
@State emailValue: string = ''
@State passwordValue: string = ''
@State phoneValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $emailValue,
placeholder: '请输入邮箱',
type: 'email',
label: '邮箱地址'
})
TextInput({
value: $passwordValue,
placeholder: '请输入密码',
type: 'password',
label: '密码'
})
TextInput({
value: $phoneValue,
placeholder: '请输入手机号',
type: 'tel',
label: '手机号'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample4 {
@State usernameValue: string = ''
@State contentValue: string = ''
@State errorValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $usernameValue,
placeholder: '请输入用户名',
label: '用户名',
hint: '用户名长度为3-20个字符'
})
TextInput({
value: $contentValue,
placeholder: '请输入内容',
label: '内容',
hint: '这是提示信息'
})
TextInput({
value: $errorValue,
placeholder: '请输入内容',
label: '内容',
errorMessage: '输入内容有误,请重新输入'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample5 {
@State requiredValue: string = ''
@State disabledValue: string = '禁用状态'
@State readonlyValue: string = '只读状态'
build() {
Column({ space: 15 }) {
TextInput({
value: $requiredValue,
placeholder: '请输入内容',
label: '必填项',
required: true
})
TextInput({
value: $disabledValue,
placeholder: '请输入内容',
label: '禁用状态',
disabled: true
})
TextInput({
value: $readonlyValue,
placeholder: '请输入内容',
label: '只读状态',
readonly: true
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample6 {
@State usernameValue: string = ''
@State emailValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $usernameValue,
placeholder: '请输入用户名',
leftTextIcon: 'U', // U = User(用户)
label: '用户名'
})
TextInput({
value: $emailValue,
placeholder: '请输入邮箱',
leftTextIcon: '@',
rightTextIcon: '✓', // ✓ = Check(确认)
label: '邮箱'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct InputExample7 {
@State inputValue: string = ''
build() {
Column({ space: 15 }) {
TextInput({
value: $inputValue,
placeholder: '最多输入10个字符',
label: '内容',
maxLength: 10,
hint: `已输入 ${this.inputValue.length}/10 个字符`
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}import { TextInput, ValidationResult } from '../components/base'
@Entry
@Component
struct InputExample8 {
@State emailValue: string = ''
// 邮箱验证函数
@Builder
validateEmail(value: string): ValidationResult {
if (!value) {
return { valid: false, message: '邮箱不能为空' }
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
if (!emailRegex.test(value)) {
return { valid: false, message: '请输入有效的邮箱地址' }
}
return { valid: true }
}
build() {
Column({ space: 15 }) {
TextInput({
value: $emailValue,
placeholder: '请输入邮箱',
label: '邮箱',
validator: this.validateEmail
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}TextInput 的所有样式都通过 ComponentTheme 配置,所有配置都在代码中,不依赖JSON文件。
import { ComponentTheme } from '../theme/ComponentTheme'
// 修改主色(影响聚焦状态的边框颜色)
ComponentTheme.PRIMARY_COLOR = '#007AFF'
// 修改错误色(影响错误状态的边框和提示颜色)
ComponentTheme.ERROR_COLOR = '#FF3B30'
// 修改边框颜色
ComponentTheme.BORDER_COLOR = '#E5E5E5'
// 修改圆角
ComponentTheme.BORDER_RADIUS = 8import { ComponentTheme } from '../theme/ComponentTheme'
// 使用 setTheme 方法批量配置
ComponentTheme.setTheme({
primaryColor: '#007AFF',
errorColor: '#FF3B30',
borderRadius: 8,
spacing: 16
})推荐:根据使用场景选择尺寸
required 属性显示红色星号validator 属性进行自定义验证ValidationResult 对象errorMessage 属性显示错误信息disabled 属性,适用于不可编辑的场景readonly 属性,适用于仅展示的场景required 属性,显示红色星号标识inputWidth 属性控制输入框宽度A: TextInput 是基础文本输入框,与其他输入框的区别:
A: 使用 @Link 和 $ 语法:
@State inputValue: string = ''
TextInput({
value: $inputValue // 使用 $ 创建双向绑定
})A: 使用 validator 属性:
@Builder
validateEmail(value: string): ValidationResult {
if (!value) {
return { valid: false, message: '邮箱不能为空' }
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
if (!emailRegex.test(value)) {
return { valid: false, message: '请输入有效的邮箱地址' }
}
return { valid: true }
}
TextInput({
value: $emailValue,
validator: this.validateEmail
})A: 设置 showClearButton 为 false:
TextInput({
value: $inputValue,
showClearButton: false
})A: 使用 inputWidth 属性:
TextInput({
value: $inputValue,
inputWidth: 300 // 固定宽度
})
TextInput({
value: $inputValue,
inputWidth: '100%' // 百分比宽度
})A: 使用 onChangeBuilder 属性:
@Builder
handleChange(value: string) {
console.info('输入值变化:', value)
// 处理输入变化
}
TextInput({
value: $inputValue,
onChangeBuilder: this.handleChange
})TextInput 是控件库中的基础文本输入框组件,具有以下核心特性:
$variableName 创建双向绑定label 属性添加标签hint 或 errorMessage 显示提示type 属性选择输入类型validator 属性进行自定义验证inputSize 属性选择合适尺寸ComponentTheme 自定义全局样式下一篇预告:PasswordInput(密码输入框)详解
本文档属于《鸿蒙PC UI控件库开发系列文章》第7篇