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

3 4
The **\<TextInput>** component provides single-line text input and is able to respond to input events.

E
ester.zhou 已提交
5 6
> **NOTE**
>
Z
zengyawen 已提交
7
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

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

None

Z
zengyawen 已提交
14 15

## Child Components
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
Not supported
Z
zengyawen 已提交
18

Z
zengyawen 已提交
19 20 21

## APIs

22
TextInput(value?:{placeholder?: string | Resource, text?: string | Resource, controller?: TextInputController})
Z
zengyawen 已提交
23 24

- Parameters
25
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
26
  | -------- | -------- | -------- | -------- | -------- |
27 28
  | placeholder | string \|&nbsp;[Resource](../../ui/ts-types.md#resource-type) | No | - | Hint text displayed when there is no input. |
  | text             | string \| [Resource](../../ui/ts-types.md#resource-type)                                   | No    | -    | Current text input. |
E
esterzhou 已提交
29
  | controller<sup>8+</sup> | [TextInputController](#textinputcontroller8) | No | - | Text input controller. |
Z
zengyawen 已提交
30 31 32 33


## Attributes

34
In addition to the universal attributes, the following attributes are supported.
Z
zengyawen 已提交
35

E
esterzhou 已提交
36
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
37
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
38
| type | InputType | InputType.Normal | Input box type. |
39
| placeholderColor | Color | - | Placeholder text color. |
E
esterzhou 已提交
40 41 42
| 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). |
E
ester.zhou 已提交
43
| maxLength | number | - | Maximum number of characters in the text input. |
44 45 46 47
| 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**: regular expression to set.<br>- **error**: error message containing the ignored content returned when regular expression matching fails. |
| copyOption<sup>9+</sup> | [CopyOptions](ts-basic-components-text.md) | CopyOptions.CrossDevice | Whether copy and paste is allowed. |
| showPasswordIcon<sup>9+</sup> | boolean | true | Whether to display the show password icon at the end of the password text box.|
| style<sup>9+</sup> | TextInputStyle | Default | Text input style.|
Z
zengyawen 已提交
48 49

- EnterKeyType enums
50
  | Name | Description |
Z
zengyawen 已提交
51
  | -------- | -------- |
52 53 54 55 56
  | 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 已提交
57 58

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

66 67 68 69 70
- TextInputStyle
  | Name                | Description           |
  | ------------------ | ------------- |
  | Default   | Default style. The caret width is fixed at 1.5 vp, and the caret height increases with the font size. |
  | Inline | Inline input style. When text is selected, its background height is the same as the height of the text box.     |
Z
zengyawen 已提交
71

E
ester.zhou 已提交
72
## Events
Z
zengyawen 已提交
73

E
esterzhou 已提交
74
| Name | Description |
Z
zengyawen 已提交
75
| -------- | -------- |
E
ester.zhou 已提交
76 77 78 79
| 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. |
80 81 82
| 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. |
E
ester.zhou 已提交
83 84 85

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

E
ester.zhou 已提交
86
Implements the controller of the **\<TextInput>** component.
Z
zengyawen 已提交
87 88


E
ester.zhou 已提交
89
#### Objects to Import
Z
zengyawen 已提交
90 91 92 93 94 95 96


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


E
ester.zhou 已提交
97
#### caretPosition
Z
zengyawen 已提交
98 99 100

caretPosition(value: number): void

E
ester.zhou 已提交
101
Sets the position of the caret.
Z
zengyawen 已提交
102 103

- Parameters
104
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
105
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
106
  | value | number | Yes | - | Length from the start of the text string to the position where the caret is located. |
Z
zengyawen 已提交
107 108


E
ester.zhou 已提交
109

Z
zengyawen 已提交
110 111 112 113 114
## Example


### Single-line Text Input

E
ester.zhou 已提交
115 116
```ts
// xxx.ets
Z
zengyawen 已提交
117 118
@Entry
@Component
E
ester.zhou 已提交
119
struct TextInputExample1 {
Z
zengyawen 已提交
120 121 122 123
  @State text: string = ''

  build() {
    Column() {
E
ester.zhou 已提交
124
      TextInput({ placeholder: 'input your word' })
Z
zengyawen 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        .placeholderColor("rgb(0,0,225)")
        .placeholderFont({ size: 30, weight: 100, family: 'cursive', style: FontStyle.Italic })
        .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)


E
ester.zhou 已提交
147
### Setting the Caret
Z
zengyawen 已提交
148

E
ester.zhou 已提交
149 150
```ts
// xxx.ets
Z
zengyawen 已提交
151 152
@Entry
@Component
E
ester.zhou 已提交
153
struct TextInputExample2 {
Z
zengyawen 已提交
154
    @State text: string = ''
Z
zengyawen 已提交
155
    controller: TextInputController = new TextInputController()
Z
zengyawen 已提交
156
    build() {
Z
zengyawen 已提交
157 158 159 160 161 162
        Column() {
            TextInput({ placeholder: 'Please input your words.', controller:this.controller})
            Button('caretPosition')
                .onClick(() => {
                this.controller.caretPosition(4)
            })
Z
zengyawen 已提交
163 164 165 166 167
        }
    }
}
```

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