• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextPicker
2
3The **\<TextPicker>** component allows users to scroll to select text.
4
5>  **NOTE**
6>
7>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Not supported
13
14
15## APIs
16
17TextPicker(options?: {range: string[]|Resource, selected?: number, value?: string})
18
19Creates a text picker based on the selection range specified by **range**.
20
21**Parameters**
22
23| Name| Type| Mandatory| Description|
24| -------- | -------- | -------- | -------- |
25| range | string[] \| [Resource](ts-types.md#resource) | Yes| Data selection range of the picker. This parameter cannot be set to an empty array. If set to an empty array, it will not be displayed. If it is dynamically changed to an empty array, the current value remains displayed.|
26| selected | number | No| Index of the default item in the range.<br>Default value: **0**|
27| value | string | No| Value of the default item in the range. The priority of this parameter is lower than that of **selected**.<br>Default value: value of the first item|
28
29## Attributes
30
31In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
32
33| Name| Type| Description|
34| -------- | -------- | -------- |
35| defaultPickerItemHeight | number \| string | Height of each item in the picker.|
36
37## Events
38
39In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
40
41| Name| Description|
42| -------- | -------- |
43| onChange(callback: (value: string, index: number) =&gt; void) | Triggered when an item in the picker is selected.<br>- **value**: value of the selected item.<br>- **index**: index of the selected item.|
44
45
46## Example
47
48```ts
49// xxx.ets
50@Entry
51@Component
52struct TextPickerExample {
53  private select: number = 1
54  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
55
56  build() {
57    Column() {
58      TextPicker({ range: this.fruits, selected: this.select })
59        .onChange((value: string, index: number) => {
60          console.info('Picker item changed, value: ' + value + ', index: ' + index)
61        })
62    }
63  }
64}
65```
66
67![en-us_image_0000001257058417](figures/en-us_image_0000001257058417.png)
68