ts-basic-components-textinput.md 6.5 KB
Newer Older
Z
zengyawen 已提交
1
# TextInput
Z
zengyawen 已提交
2 3


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9 10 11

The **<TextInput>** component provides single-line text input.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

None

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

TextInput(value?:{placeholder?: string controller?: TextInputController})

- Parameters
E
esterzhou 已提交
26
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
28 29
  | placeholder | string | No | - | Text displayed when there is no input. |
  | controller<sup>8+</sup> | [TextInputController](#textinputcontroller8) | No | - | Text input controller. |
Z
zengyawen 已提交
30 31 32 33


## Attributes

E
ester.zhou 已提交
34
In addition to [universal attributes](ts-universal-attributes-index.md), the following attributes are supported.
Z
zengyawen 已提交
35

E
esterzhou 已提交
36
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
38 39 40 41 42 43
| type | InputType | InputType.Normal | Input box type. |
| placeholderColor | Color | - | Placeholder color. |
| placeholderFont | {<br/>size?: Length,<br/>weight?: number\|[FontWeight](ts-universal-attributes-text-style.md),<br/>family?: string,<br/>style?: [FontStyle](ts-universal-attributes-text-style.md)<br/>} | - | Placeholder text style.<br/>- **size**: font size. If the value is of the number type, the unit fp is used.<br/>- **weight**: font weight. For the number type, the value ranges from 100 to 900, at an interval of 100. The default value is **400**. A larger value indicates a larger font weight.<br/>- **family**: font family. Use commas (,) to separate multiple fonts, for example, **'Arial, sans-serif'**. The priority of the fonts is the sequence in which they are placed.<br/>- **style**: font style. |
| enterKeyType | EnterKeyType | EnterKeyType.Done | How the Enter key is labeled. |
| caretColor | Color | - | Color of the caret (also known as the text insertion cursor). |
| maxLength<sup>8+</sup> | number | - | Maximum number of characters in the text input. |
E
esterzhou 已提交
44
| inputFilter<sup>8+</sup> | {<br/>value: [ResourceStr](../../ui/ts-types.md)<sup>8+</sup>,<br/>error?: (value: string)<br/>} | - | Regular expression for input filtering. Only inputs that comply with the regular expression can be displayed. Other inputs are ignored. The specified regular expression can match single characters, but not strings. Example: ^(? =.\*\d)(? =.\*[a-z])(? =.\*[A-Z]).{8,10}$. Strong passwords containing 8 to 10 characters cannot be filtered.<br/>- **value**: indicates the regular expression to set.<br/>- **error**: returns the ignored content when regular expression matching fails. |
Z
zengyawen 已提交
45 46

- EnterKeyType enums
E
esterzhou 已提交
47
    | Name | Description |
Z
zengyawen 已提交
48
  | -------- | -------- |
E
esterzhou 已提交
49 50 51 52 53
  | EnterKeyType.Go | The Enter key is labeled "Go." |
  | EnterKeyType.Search | The Enter key is labeled "Search." |
  | EnterKeyType.Send | The Enter key is labeled "Send." |
  | EnterKeyType.Next | The Enter key is labeled "Next." |
  | EnterKeyType.Done | The Enter key is labeled "Done." |
Z
zengyawen 已提交
54 55

- InputType enums
E
esterzhou 已提交
56
    | Name | Description |
Z
zengyawen 已提交
57
  | -------- | -------- |
E
esterzhou 已提交
58 59 60 61
  | InputType.Normal | Normal input mode. |
  | InputType.Password | Password input mode. |
  | InputType.Email | Email address input mode. |
  | InputType.Number | Digit input mode. |
Z
zengyawen 已提交
62 63 64 65 66 67


### TextInputController<sup>8+</sup>

Implements the controller of the **&lt;TextInput&gt;** component.

E
esterzhou 已提交
68
| Name | Description |
Z
zengyawen 已提交
69
| -------- | -------- |
E
esterzhou 已提交
70
| caretPosition(value: number):void | Position of the input cursor.<br/>**value**: indicates the length from the start of the string to the position where the input cursor is located. |
Z
zengyawen 已提交
71 72 73 74 75 76 77 78 79 80


### Objects to Import


```
controller: TextInputController = new TextInputController()
```


E
ester.zhou 已提交
81
### controller.createPosition
Z
zengyawen 已提交
82 83 84 85 86 87

caretPosition(value: number): void

Sets the cursor in a specified position.

- Parameters
E
esterzhou 已提交
88
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
89
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
90
  | value | number | Yes | - | Position of the input cursor.<br/>**value**: indicates the length from the start of the string to the position where the input cursor is located. |
Z
zengyawen 已提交
91 92


E
ester.zhou 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
## Events

| Name | Description |
| -------- | -------- |
| onChange(value: string) =&gt; void | Triggered when the input changes. |
| onSubmit(callback: (enterKey: EnterKeyType) =&gt; void) | Triggered when the Enter key on the physical or soft keyboard is pressed. |
| onEditChanged(callback:&nbsp;(isEditing:&nbsp;boolean)&nbsp;=&gt;&nbsp;void)<sup>(deprecated) </sup> | Triggered when the input status changes. |
| onEditChange(callback:&nbsp;(isEditing:&nbsp;boolean)&nbsp;=&gt;&nbsp;void) <sup>8+</sup> | Triggered when the input status changes. |
| onCopy<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the copy button on the pasteboard, which displays when the text box is long pressed, is clicked.<br/>**value**: text to be copied. |
| onCut<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the cut button on the pasteboard, which displays when the text box is long pressed, is clicked.<br/>**value**: text to be cut. |
| onPaste<sup>8+</sup>(callback:(value: string) =&gt; void) | Triggered when the paste button on the pasteboard, which displays when the text box is long pressed, is clicked.<br/>**value**: text to be pasted. |


Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
## Example


### Single-line Text Input


```
@Entry
@Component
struct TextAreaExample2 {
  @State text: string = ''

  build() {
    Column() {
      TextArea({ placeholder: 'input your word' })
        .placeholderColor("rgb(0,0,225)")
        .placeholderFont({ size: 30, weight: 100, family: 'cursive', style: FontStyle.Italic })
        .textAlign(TextAlign.Center)
        .caretColor(Color.Blue)
        .height(50)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .fontFamily("sans-serif")
        .fontStyle(FontStyle.Normal)
        .fontColor(Color.Red)
        .onChange((value: string) => {
          this.text = value
        })
      Text(this.text).width('90%')
    }
  }
}
```


![en-us_image_0000001212378402](figures/en-us_image_0000001212378402.gif)


### Setting the Input Cursor

Z
zengyawen 已提交
146 147 148 149 150 151

```
@Entry
@Component
struct TextInputTest {
    @State text: string = ''
Z
zengyawen 已提交
152
    controller: TextInputController = new TextInputController()
Z
zengyawen 已提交
153
    build() {
Z
zengyawen 已提交
154 155 156 157 158 159
        Column() {
            TextInput({ placeholder: 'Please input your words.', controller:this.controller})
            Button('caretPosition')
                .onClick(() => {
                this.controller.caretPosition(4)
            })
Z
zengyawen 已提交
160 161 162 163 164
        }
    }
}
```

Z
zengyawen 已提交
165
![en-us_image_0000001212058468](figures/en-us_image_0000001212058468.png)