ts-basic-components-textpicker.md 1.9 KB
Newer Older
Z
zengyawen 已提交
1 2
# TextPicker

E
ester.zhou 已提交
3
The **TextPicker** component allows users to scroll to select text.
Z
zengyawen 已提交
4

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


E
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

12
Not supported
Z
zengyawen 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
TextPicker(options?: {range: string[]|Resource, selected?: number, value?: string})
Z
zengyawen 已提交
18 19 20

Creates a text picker based on the selection range specified by **range**.

E
ester.zhou 已提交
21
**Parameters**
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25 26 27
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| range | string[] \| [Resource](ts-types.md#resource)| Yes| Data selection range of the picker.|
| selected | number | No| Index of the selected item in the range.<br>Default value: **0**|
| value | string | No| Value of the selected item. The priority of this parameter is lower than that of **selected**.<br>Default value: value of the first item|
Z
zengyawen 已提交
28 29 30

## Attributes

E
ester.zhou 已提交
31 32 33
| Name| Type| Description|
| -------- | -------- | -------- |
| defaultPickerItemHeight | number \| string | Default height of an item in the picker.|
Z
zengyawen 已提交
34 35 36

## Events

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

E
ester.zhou 已提交
39
| Name| Description|
E
esterzhou 已提交
40
| -------- | -------- |
E
ester.zhou 已提交
41
| 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.|
Z
zengyawen 已提交
42 43 44 45


## Example

46 47
```ts
// xxx.ets
Z
zengyawen 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
@Entry
@Component
struct TextPickerExample {
  private select: number = 1
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']

  build() {
    Column() {
      TextPicker({range: this.fruits, selected: this.select})
        .onChange((value: string, index: number) => {
          console.info('Picker item changed, value: ' + value + ', index: ' + index)
        })
    }
  }
}
```

![en-us_image_0000001257058417](figures/en-us_image_0000001257058417.png)