1# TextInput 2 3单行文本输入框组件。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12无 13 14 15## 接口 16 17TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController}) 18 19**参数:** 20 21| 参数名 | 参数类型 | 必填 | 参数描述 | 22| ----------------------- | ---------------------------------------- | ---- | --------------- | 23| placeholder | [ResourceStr](ts-types.md#resourcestr) | 否 | 设置无输入时的提示文本。 | 24| text | [ResourceStr](ts-types.md#resourcestr) | 否 | 设置输入框当前的文本内容。</br>当组件设置[stateStyles](ts-universal-attributes-polymorphic-style.md)等刷新属性时,建议通过onChange事件将状态变量与文本实时绑定,</br>避免组件刷新时TextInput中的文本内容异常。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 25| controller<sup>8+</sup> | [TextInputController](#textinputcontroller8) | 否 | 设置TextInput控制器。 | 26 27 28## 属性 29 30除支持[通用属性](ts-universal-attributes-size.md)和[文本通用属性](ts-universal-attributes-text-style.md)的fontColor、fontSize、fontStyle、fontWeight、fontFamily外,还支持以下属性: 31 32| 名称 | 参数类型 | 描述 | 33| ------------------------ | ---------------------------------------- | ---------------------------------------- | 34| type | [InputType](#inputtype枚举说明) | 设置输入框类型。<br/>默认值:InputType.Normal | 35| placeholderColor | [ResourceColor](ts-types.md#resourcecolor) | 设置placeholder文本颜色。<br/>默认值跟随主题。 | 36| placeholderFont | [Font](ts-types.md#font) | 设置placeholder文本样式。 | 37| enterKeyType | [EnterKeyType](#enterkeytype枚举说明) | 设置输入法回车键类型。<br/>默认值:EnterKeyType.Done | 38| caretColor | [ResourceColor](ts-types.md#resourcecolor) | 设置输入框光标颜色。<br/>默认值:'#007DFF'。 | 39| maxLength | number | 设置文本的最大输入字符数。 | 40| inputFilter<sup>8+</sup> | {<br/>value: [ResourceStr](ts-types.md#resourcestr),<br/>error?: (value: string) => void<br/>} | 正则表达式,匹配表达式的输入允许显示,不匹配的输入将被过滤。目前仅支持单个字符匹配,不支持字符串匹配。<br/>- value:设置正则表达式。<br/>- error:正则匹配失败时,返回被过滤的内容。 | 41| copyOption<sup>9+</sup> | [CopyOptions](ts-appendix-enums.md#copyoptions9) | 设置输入的文本是否可复制。<br/>默认值:CopyOptions.LocalDevice,支持设备内复制。<br/>设置CopyOptions.None时,当前TextInput中的文字无法被复制或剪切,仅支持粘贴。<br/>**说明:**<br/>copyOption对于拖拽,只限制是否选中,不涉及拖拽范围。 | 42| showPasswordIcon<sup>9+</sup> | boolean | 密码输入模式时,输入框末尾的图标是否显示。<br/>默认值:true | 43| style<sup>9+</sup> | [TextInputStyle](#textinputstyle9枚举说明) \| [TextContentStyle](ts-appendix-enums.md#textcontentstyle10) | 设置输入框为默认风格或内联输入风格(内联输入风格只支持InputType.Normal类型)。<br/>默认值:TextInputStyle.Default | 44| textAlign<sup>9+</sup> | [TextAlign](ts-appendix-enums.md#textalign) | 设置文本在输入框中的水平对齐方式。<br/>默认值:TextAlign.Start<br/>**说明:**<br/>仅支持TextAlign.Start、TextAlign.Center和TextAlign.End。<br/>可通过[align](ts-universal-attributes-location.md)属性控制文本段落在垂直方向上的位置,此组件中不可通过align属性控制文本段落在水平方向上的位置,即align属性中Alignment.TopStart、Alignment.Top、Alignment.TopEnd效果相同,控制内容在顶部,Alignment.Start、Alignment.Center、Alignment.End效果相同,控制内容垂直居中,Alignment.BottomStart、Alignment.Bottom、Alignment.BottomEnd效果相同,控制内容在底部。 | 45| selectedBackgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 设置文本选中底板颜色。<br/>如果未设置不透明度,默认为20%不透明度。 | 46| caretStyle<sup>10+</sup> | {<br/>width: [Length](ts-types.md#length)<br/>} | 设置光标风格,不支持百分比设置。 | 47| caretPosition<sup>10+</sup> | number | 设置光标位置。 | 48| showUnit<sup>10+</sup> | [CustomBuilder](ts-types.md#custombuilder8) | 设置控件作为文本框单位。<br/>默认无单位。<br/>需搭配showUnderline使用,当showUnderline为true时生效。 | 49| showError<sup>10+</sup> | string \| undefined | 设置错误状态下提示的错误文本或者不显示错误状态。<br/>默认不显示错误状态。<br/>**说明:** <br/>当参数类型为string并且输入内容不符合定义规范时,提示错误文本。当参数类型为undefined时,不显示错误状态。请参考[示例2](#示例2) | 50| showUnderline<sup>10+</sup> | boolean | 设置是否开启下划线。下划线默认颜色为'#33182431',默认粗细为1px,文本框尺寸48vp(下划线只支持InputType.Normal类型)。<br/>默认值:false | 51| passwordIcon<sup>10+</sup> | [PasswordIcon](#passwordicon10对象说明) | 密码输入模式时,设置输入框末尾的图标。<br/>默认为系统提供的密码图标。 | 52| enableKeyboardOnFocus<sup>10+</sup> | boolean | TextInput通过点击以外的方式获焦时,是否绑定输入法。<br/>默认值:true。从API version 10开始,获焦默认绑定输入法。 | 53| selectionMenuHidden<sup>10+</sup> | boolean | 设置长按输入框或者右键输入框时,是否弹出文本选择菜单。<br />默认值:false | 54| barState<sup>10+</sup> | [BarState](ts-appendix-enums.md#barstate) | 设置内联输入风格编辑态时滚动条的显示模式。<br/>默认值:BarState.Auto | 55| maxLines<sup>10+</sup> | number | 设置内联输入风格编辑态时文本可显示的最大行数。<br/>默认值:3 <br/>**说明:**<br/>取值范围:(0, +∞)。| 56| customKeyboard<sup>10+</sup> | [CustomBuilder](ts-types.md#custombuilder8) | 设置自定义键盘。<br/>**说明:**<br/>当设置自定义键盘时,输入框激活后不会打开系统输入法,而是加载指定的自定义组件,针对系统键盘的enterKeyType属性设置将无效。<br/>自定义键盘的高度可以通过自定义组件根节点的height属性设置,宽度不可设置,使用系统默认值。<br/>自定义键盘采用覆盖原始界面的方式呈现,不会对应用原始界面产生压缩或者上提。<br/>自定义键盘无法获取焦点,但是会拦截手势事件。<br/>默认在输入控件失去焦点时,关闭自定义键盘,开发者也可以通过[TextInputController](#textinputcontroller8).[stopEditing](#stopediting10)方法控制键盘关闭。 | 57> **说明:** 58> [通用属性padding](ts-universal-attributes-size.md)的默认值为:<br>{<br> top: 8 vp,<br> right: 16 vp,<br> bottom: 8 vp,<br> left: 16 vp<br> } 59> 从API version 10开始,单行输入框可设置.width('auto')使组件宽度自适应文本宽度,自适应时组件宽度受constraintSize属性以及父容器传递的最大最小宽度限制,其余使用方式参考[尺寸设置](ts-universal-attributes-size.md#属性)。 60 61## EnterKeyType枚举说明 62 63| 名称 | 描述 | 64| ------------------- | --------- | 65| Go | 显示为开始样式。 | 66| Search | 显示为搜索样式。 | 67| Send | 显示为发送样式。 | 68| Next | 显示为下一个样式。 | 69| Done | 显示为换行样式。 | 70 71## InputType枚举说明 72 73| 名称 | 描述 | 74| ------------------ | ------------- | 75| Normal | 基本输入模式,无特殊限制。 | 76| Password | 密码输入模式。支持输入数字、字母、下划线、空格、特殊字符。密码显示小眼睛图标并且默认会将文字变成圆点。密码输入模式不支持下划线样式。 | 77| Email | 邮箱地址输入模式。支持数字,字母,下划线,小数点,以及@字符(只能存在一个@字符)。 | 78| Number | 纯数字输入模式。 | 79| PhoneNumber<sup>9+</sup> | 电话号码输入模式。<br/>支持输入数字、+ 、-、*、#,长度不限。 | 80 81## TextInputStyle<sup>9+</sup>枚举说明 82 83| 名称 | 描述 | 84| ------------------ | ------------- | 85| Default | 默认风格,光标宽1.5vp,光标高度与文本选中底板高度和字体大小相关。 | 86| Inline | 内联输入风格。文本选中底板高度与输入框高度相同。<br/>内联输入是在有明显的编辑态/非编辑态的区分场景下使用,例如:文件列表视图中的重命名。<br/>不支持showError属性。 | 87 88## PasswordIcon<sup>10+</sup>对象说明 89 90| 名称 | 类型 | 必填 | 描述 | 91| ---------- | -------------------------------------------------------- | ---- | -------------------------------------------------- | 92| onIconSrc | string \| [Resource](ts-types.md#resource类型) | 否 | 密码输入模式时,能够切换密码隐藏的显示状态的图标。 | 93| offIconSrc | string \| [Resource](ts-types.md#resource类型) | 否 | 密码输入模式时,能够切换密码显示的隐藏状态的图标。 | 94 95## 事件 96 97除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 98 99| 名称 | 功能描述 | 100| ------------------------------------------------------------ | ------------------------------------------------------------ | 101| onChange(callback: (value: string) => void) | 输入内容发生变化时,触发该回调。<br/>value:输入的文本内容。<br/>触发该事件的条件:<br/>1、键盘输入。<br/>2、粘贴、剪切。<br/>3、键盘快捷键Ctrl+v。 | 102| onSubmit(callback: (enterKey: EnterKeyType) => void) | 按下输入法回车键触发该回调,返回值为当前输入法回车键的类型。<br/>enterKeyType:输入法回车键类型。具体类型见[EnterKeyType枚举说明](#enterkeytype枚举说明)。 | 103| onEditChanged(callback: (isEditing: boolean) => void)<sup>(deprecated)</sup> | 输入状态变化时,触发该回调。从API 8开始,建议使用onEditChange。 | 104| onEditChange(callback: (isEditing: boolean) => void)<sup>8+</sup> | 输入状态变化时,触发该回调。有光标时为编辑态,无光标时为非编辑态。isEditing为true表示正在输入。 | 105| onCopy(callback:(value: string) => void)<sup>8+</sup> | 长按输入框内部区域弹出剪贴板后,点击剪切板复制按钮,触发该回调。<br/>value:复制的文本内容。 | 106| onCut(callback:(value: string) => void)<sup>8+</sup> | 长按输入框内部区域弹出剪贴板后,点击剪切板剪切按钮,触发该回调。<br/>value:剪切的文本内容。 | 107| onPaste(callback:(value: string) => void)<sup>8+</sup> | 长按输入框内部区域弹出剪贴板后,点击剪切板粘贴按钮,触发该回调。<br/>value:粘贴的文本内容。 | 108| onTextSelectionChange(callback: (selectionStart: number, selectionEnd: number) => void)<sup>10+</sup> | 文本选择的位置发生变化时,触发该回调。<br />selectionStart:文本选择区域起始位置,文本框中文字的起始位置为0。<br />selectionEnd:文本选择区域结束位置。 | 109| onContentScroll(callback: (totalOffsetX: number, totalOffsetY: number) => void)<sup>10+</sup> | 文本内容滚动时,触发该回调。<br />totalOffsetX:文本在内容区的横坐标偏移,单位px。<br />totalOffsetY:文本在内容区的纵坐标偏移,单位px。 | 110 111## TextInputController<sup>8+</sup> 112 113TextInput组件的控制器。 114 115### 导入对象 116``` 117controller: TextInputController = new TextInputController() 118``` 119### caretPosition<sup>8+</sup> 120 121caretPosition(value: number): void 122 123设置输入光标的位置。 124 125**参数:** 126 127| 参数名 | 参数类型 | 必填 | 参数描述 | 128| ------ | -------- | ---- | -------------------------------------- | 129| value | number | 是 | 从字符串开始到光标所在位置的字符长度。 | 130### setTextSelection<sup>10+</sup> 131 132setTextSelection(selectionStart: number, selectionEnd: number): void 133 134设置文本选择区域并高亮显示。 135 136**参数:** 137 138| 参数名 | 参数类型 | 必填 | 参数描述 | 139| -------------- | -------- | ---- | ---------------------- | 140| selectionStart | number | 是 | 文本选择区域起始位置,文本框中文字的起始位置为0。 | 141| selectionEnd | number | 是 | 文本选择区域结束位置。 | 142> **说明:** 143> 144> 如果selectionStart或selectionEnd被赋值为undefined时,当作0处理。 145### stopEditing<sup>10+</sup> 146 147stopEditing(): void 148 149退出编辑态。 150 151### getTextContentRect<sup>10+</sup> 152 153getTextContentRect(): [RectResult](#rectresult10) 154 155获取已编辑文本内容区域相对组件的位置和大小,返回值单位为像素。 156 157**返回值:** 158 159| 类型 | 说明 | 160| ------------------- | -------- | 161| [RectResult](#rectresult10) | 已编辑文本内容的相对组件的位置和大小。 | 162 163> **说明:** 164> 165> - 初始不输入文本时,返回值中有相对组件的位置信息,大小为0。 166> - 返回值中的位置信息是第一个字符相对于可编辑组件的位置。 167 168### RectResult<sup>10+</sup> 169 170位置和大小,单位均为像素。 171 172| 参数 | 类型 | 描述 | 173| ------- | ------ | ----------------------- | 174| x | number | 水平方向横坐标。| 175| y | number | 竖直方向纵坐标。| 176| width | number | 内容宽度大小。| 177| height | number | 内容高度大小。| 178 179 180### getTextContentLineCount<sup>10+</sup> 181 182getTextContentLineCount(): number 183 184获取已编辑文本内容的行数。 185 186**返回值:** 187 188| 类型 | 说明 | 189| ----- | -------- | 190| number| 已编辑文本内容行数。 | 191 192## 示例 193 194### 示例1 195 196```ts 197// xxx.ets 198@Entry 199@Component 200struct TextInputExample { 201 @State text: string = '' 202 controller: TextInputController = new TextInputController() 203 204 build() { 205 Column() { 206 TextInput({ text: this.text, placeholder: 'input your word...', controller: this.controller }) 207 .placeholderColor(Color.Grey) 208 .placeholderFont({ size: 14, weight: 400 }) 209 .caretColor(Color.Blue) 210 .width('95%') 211 .height(40) 212 .margin(20) 213 .fontSize(14) 214 .fontColor(Color.Black) 215 .inputFilter('[a-z]', (e) => { 216 console.log(JSON.stringify(e)) 217 }) 218 .onChange((value: string) => { 219 this.text = value 220 }) 221 Text(this.text) 222 Button('Set caretPosition 1') 223 .margin(15) 224 .onClick(() => { 225 // 将光标移动至第一个字符后 226 this.controller.caretPosition(1) 227 }) 228 // 密码输入框 229 TextInput({ placeholder: 'input your password...' }) 230 .width('95%') 231 .height(40) 232 .margin(20) 233 .type(InputType.Password) 234 .maxLength(9) 235 .showPasswordIcon(true) 236 // 内联风格输入框 237 TextInput({ text: 'inline style' }) 238 .width('95%') 239 .height(50) 240 .margin(20) 241 .borderRadius(0) 242 .style(TextInputStyle.Inline) 243 }.width('100%') 244 } 245} 246``` 247 248 249 250### 示例2 251 252```ts 253@Entry 254@Component 255struct TextInputExample { 256 @State PassWordSrc1: Resource = $r('app.media.onIcon') 257 @State PassWordSrc2: Resource = $r('app.media.offIcon') 258 @State TextError: string = '' 259 @State Text: string = '' 260 @State NameText: string = 'test' 261 262 @Builder itemEnd() { 263 Select([{ value: 'KB' }, 264 { value: 'MB' }, 265 { value: 'GB' }, 266 { value: 'TB', }]) 267 .height("48vp") 268 .borderRadius(0) 269 .selected(2) 270 .align(Alignment.Center) 271 .value('MB') 272 .font({ size: 20, weight: 500 }) 273 .fontColor('#182431') 274 .selectedOptionFont({ size: 20, weight: 400 }) 275 .optionFont({ size: 20, weight: 400 }) 276 .backgroundColor(Color.Transparent) 277 .responseRegion({ height: "40vp", width: "80%", x: '10%', y: '6vp' }) 278 .onSelect((index: number) => { 279 console.info('Select:' + index) 280 }) 281 } 282 283 build() { 284 Column({ space: 20 }) { 285 // 自定义密码显示图标 286 TextInput({ placeholder: 'user define password icon' }) 287 .type(InputType.Password) 288 .width(380) 289 .height(60) 290 .passwordIcon({ onIconSrc: this.PassWordSrc1, offIconSrc: this.PassWordSrc2 }) 291 // 下划线模式 292 TextInput({ placeholder: 'underline style' }) 293 .showUnderline(true) 294 .width(380) 295 .height(60) 296 .showError('Error') 297 .showUnit(this.itemEnd) 298 299 Text(`用户名:${this.Text}`) 300 .width('95%') 301 TextInput({ placeholder: '请输入用户名', text: this.Text }) 302 .showUnderline(true) 303 .width(380) 304 .showError(this.TextError) 305 .onChange((value: string) => { 306 this.Text = value 307 }) 308 .onSubmit(() => { // 用户名不正确会清空输入框和用户名并提示错误文本 309 if (this.Text == this.NameText) { 310 this.TextError = '' 311 } else { 312 this.TextError = '用户名输入错误' 313 this.Text = '' 314 } 315 }) 316 317 }.width('100%') 318 } 319} 320``` 321 322 323 324### 示例3 325 326```ts 327// xxx.ets 328@Entry 329@Component 330struct TextInputExample { 331 controller: TextInputController = new TextInputController() 332 @State inputValue: string = "" 333 334 // 自定义键盘组件 335 @Builder CustomKeyboardBuilder() { 336 Column() { 337 Button('x').onClick(() => { 338 // 关闭自定义键盘 339 this.controller.stopEditing() 340 }) 341 Grid() { 342 ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item:number|string) => { 343 GridItem() { 344 Button(item + "") 345 .width(110).onClick(() => { 346 this.inputValue += item 347 }) 348 } 349 }) 350 }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 351 }.backgroundColor(Color.Gray) 352 } 353 354 build() { 355 Column() { 356 TextInput({ controller: this.controller, text: this.inputValue }) 357 // 绑定自定义键盘 358 .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) 359 } 360 } 361} 362``` 363 364 365 366