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

E
ester.zhou 已提交
3
The **\<TextInput>** component provides single-line text input.
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
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

## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## APIs

E
ester.zhou 已提交
17
TextInput(value?:{placeholder?: ResourceStr, text?: ResourceStr, controller?: TextInputController})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22
**Parameters**

| Name                    | Type                                    | Mandatory  | Description           |
| ----------------------- | ---------------------------------------- | ---- | --------------- |
E
ester.zhou 已提交
23
| placeholder   | [ResourceStr](ts-types.md#resourcestr)       | No   | Placeholder text displayed when there is no input.     |
E
ester.zhou 已提交
24 25
| text          | [ResourceStr](ts-types.md#resourcestr)       | No   | Current text input.    |
| controller<sup>8+</sup> | [TextInputController](#textinputcontroller8) | No   | Text input controller.|
Z
zengyawen 已提交
26 27 28 29


## Attributes

E
ester.zhou 已提交
30
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
E
ester.zhou 已提交
31 32 33

| Name                      | Type                                    | Description                                      |
| ------------------------ | ---------------------------------------- | ---------------------------------------- |
E
ester.zhou 已提交
34
| type                     | InputType     | Input box type.<br>Default value: **InputType.Normal**       |
E
ester.zhou 已提交
35
| placeholderColor         | [ResourceColor](ts-types.md#resourcecolor)     | Placeholder text color.|
E
ester.zhou 已提交
36
| placeholderFont          | [Font](ts-types.md#font) | Placeholder text font.|
E
ester.zhou 已提交
37
| enterKeyType             | EnterKeyType | Type of the Enter key. Only the default value is supported.<br>Default value: **EnterKeyType.Done**|
E
ester.zhou 已提交
38 39
| caretColor               | [ResourceColor](ts-types.md#resourcecolor)    | Color of the caret in the text box.                              |
| maxLength                | number                                   | Maximum number of characters in the text input.                           |
E
ester.zhou 已提交
40
| inputFilter<sup>8+</sup> | {<br>value: [ResourceStr](ts-types.md#resourcestr),<br>error?: (value: string) =&gt; void<br>} | Regular expression for input filtering. Only inputs that comply with the regular expression can be displayed. Other inputs are filtered out. The regular expression can match single characters, but not strings.<br>- **value**: regular expression to set.<br>- **error**: filtered-out content to return when regular expression matching fails.|
41
| copyOption<sup>9+</sup>  | [CopyOptions](ts-appendix-enums.md#copyoptions9) | Whether copy and paste is allowed.<br>If this attribute is set to **CopyOptions.None**, the paste operation is allowed, but not the copy or cut operation.|
E
ester.zhou 已提交
42 43
| showPasswordIcon<sup>9+</sup> | boolean | Whether to display the show password icon at the end of the password text box.<br>Default value: **true**|
| style<sup>9+</sup> | TextInputStyle | Text input style.<br>Default value: **TextInputStyle.Default**|
E
ester.zhou 已提交
44
| textAlign<sup>9+</sup>   | [TextAlign](ts-appendix-enums.md#textalign) | Alignment mode of the text in the text box.<br>Default value: **TextAlign.Start** |
E
ester.zhou 已提交
45

E
ester.zhou 已提交
46 47 48 49
>  **NOTE**
>
>  The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows: <br>{<br> top: 8 vp,<br> right: 16 vp,<br> bottom: 16 vp,<br> left: 8 vp<br> }

E
ester.zhou 已提交
50 51 52 53 54 55 56 57 58 59
## EnterKeyType

| Name                 | Description       |
| ------------------- | --------- |
| Go     | The Enter key is labeled "Go."  |
| Search | The Enter key is labeled "Search." |
| Send   | The Enter key is labeled "Send." |
| Next   | The Enter key is labeled "Next."|
| Done   | The Enter key is labeled "Done."    |

60
## InputType
E
ester.zhou 已提交
61 62 63 64 65 66 67 68 69

| Name                | Description           |
| ------------------ | ------------- |
| Normal   | Normal input mode.<br>The value can contain digits, letters, underscores (_), spaces, and special characters.|
| Password | Password input mode.      |
| Email    | Email address input mode.|
| Number   | Digit input mode.     |
| PhoneNumber<sup>9+</sup> | Phone number input mode.<br>The value can contain digits, plus signs (+), hyphens (-), asterisks (*), and number signs (#). The length is not limited.|

E
ester.zhou 已提交
70
## TextInputStyle<sup>9+</sup>
E
ester.zhou 已提交
71 72 73

| Name                | Description           |
| ------------------ | ------------- |
E
ester.zhou 已提交
74 75
| Default   | Default style. The caret width is fixed at 1.5 vp, and the caret height is subject to the background height and font size of the selected text.  |
| Inline    | Inline input style. The background height of the selected text is the same as the height of the text box.     |
E
ester.zhou 已提交
76

77
## Events
E
ester.zhou 已提交
78

E
ester.zhou 已提交
79 80
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

E
ester.zhou 已提交
81 82 83 84 85 86 87 88 89
| Name                                                         | Description                                                  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| onChange(callback: (value: string) =&gt; void)               | Triggered when the input changes.                            |
| onSubmit(callback: (enterKey: EnterKeyType) =&gt; void)      | Triggered when the Enter key on the keyboard is pressed. The return value is the current type of the Enter key. |
| onEditChanged(callback: (isEditing: boolean) =&gt; void)<sup>(deprecated)</sup> | Triggered when the input status changes. Since API version 8, **onEditChange** is recommended. |
| onEditChange(callback: (isEditing: boolean) =&gt; void)<sup>8+</sup> | Triggered when the input status changes. If the value of **isEditing** is **true**, text input is in progress. |
| onCopy(callback:(value: string) =&gt; void)<sup>8+</sup>     | 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(callback:(value: string) =&gt; void)<sup>8+</sup>      | 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(callback:(value: string) =&gt; void)<sup>8+</sup>    | 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 已提交
90 91

## TextInputController<sup>8+</sup>
E
ester.zhou 已提交
92

E
ester.zhou 已提交
93
Implements the controller of the **\<TextInput>** component.
Z
zengyawen 已提交
94

E
ester.zhou 已提交
95
### Objects to Import
Z
zengyawen 已提交
96 97 98
```
controller: TextInputController = new TextInputController()
```
E
ester.zhou 已提交
99
### caretPosition
Z
zengyawen 已提交
100 101 102

caretPosition(value: number): void

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

E
ester.zhou 已提交
105
**Parameters**
Z
zengyawen 已提交
106

E
ester.zhou 已提交
107 108 109
| Name| Type| Mandatory| Description                              |
| ------ | -------- | ---- | -------------------------------------- |
| value  | number   | Yes  | Length from the start of the string to the position where the caret is located.|
Z
zengyawen 已提交
110

E
ester.zhou 已提交
111

Z
zengyawen 已提交
112 113
## Example

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

  build() {
    Column() {
E
ester.zhou 已提交
124 125 126
      TextInput({ placeholder: 'input your word...', controller: this.controller })
        .placeholderColor(Color.Grey)
        .placeholderFont({ size: 14, weight: 400 })
Z
zengyawen 已提交
127
        .caretColor(Color.Blue)
E
ester.zhou 已提交
128 129 130 131 132
        .width(400)
        .height(40)
        .margin(20)
        .fontSize(14)
        .fontColor(Color.Black)
Z
zengyawen 已提交
133 134 135
        .onChange((value: string) => {
          this.text = value
        })
E
ester.zhou 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      Text(this.text)
      Button('Set caretPosition 1')
        .margin(15)
        .onClick(() => {
          // Move the caret to after the first entered character.
          this.controller.caretPosition(1)
        })
      // Password text box.
      TextInput({ placeholder: 'input your password...' })
        .width(400)
        .height(40)
        .margin(20)
        .type(InputType.Password)
        .maxLength(9)
        .showPasswordIcon(true)
      // Inline-style text box.
      TextInput({ placeholder: 'inline style' })
        .width(400)
        .height(50)
        .margin(20)
        .borderRadius(0)
        .style(TextInputStyle.Inline)
    }.width('100%')
Z
zengyawen 已提交
159 160 161 162
  }
}
```

E
ester.zhou 已提交
163
![textInput](figures/textInput.gif)