ts-basic-components-textpicker.md 2.0 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# TextPicker


> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


The **<TextPicker>** component allows users to select text from a list of options.


## Required Permissions

None


## Child Component

None


## APIs

TextPicker(value: {range: string[], selected?: number})

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

- Parameter
    | Name | Type | Mandatory | Default Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | range | string[] | Yes | - | Data selection range of the picker. | 
  | selected | number | No | First element | Index value of the selected item in the array. | 


## Attributes

  | Name | Type | Default Value | Description | 
| -------- | -------- | -------- | -------- |
| defaultPickerItemHeight | Length | - | Default height of a Picker content item element. | 


## Events

  | Name | Description | 
| -------- | -------- | 
| onChange(callback:&nbsp;(value:&nbsp;string,&nbsp;index:&nbsp;number)&nbsp;=&gt;&nbsp;void) | Triggered&nbsp;when&nbsp;an&nbsp;item&nbsp;in&nbsp;the&nbsp;picker&nbsp;is&nbsp;selected.<br/>-&nbsp;**value**:&nbsp;text&nbsp;of&nbsp;the&nbsp;selected&nbsp;item.<br/>-&nbsp;**index**:&nbsp;index&nbsp;of&nbsp;the&nbsp;selected&nbsp;item. | 


## Example


```
@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)