ts-methods-textpicker-dialog.md 2.2 KB
Newer Older
E
esterzhou 已提交
1 2
# Text Picker Dialog Box

3
> **NOTE**<br>
E
esterzhou 已提交
4 5 6 7 8 9 10 11 12 13
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.

You can display a text picker in a dialog box to allow users to select text from the given range.

## Required Permissions

None

## TextPickerDialog.show

E
ester.zhou 已提交
14
show(options: TextPickerDialogOptions)
E
esterzhou 已提交
15 16 17

Shows a text picker in the given settings.

E
ester.zhou 已提交
18
- TextPickerDialogOptions
19
  | Name | Type | Mandatory | Default Value | Description |
E
esterzhou 已提交
20
  | -------- | -------- | -------- | -------- | -------- |
21 22 23 24 25 26
  | range | string[] | Yes | - | Data selection range of the picker. |
  | selected | number | No | First element | Index value of the selected item in the range. |
  | defaultPickerItemHeight | number | No | - | Height of the default selected item in the picker. |
  | onAccept | (value: TextPickerResult) => void | No | - | Callback invoked when the OK button in the dialog box is clicked. |
  | onCancel | () => void | No | - | Triggered when the Cancel button in the dialog box is clicked. |
  | onChange | (value: TextPickerResult) => void | No | - | Callback invoked when the selected item in the picker changes. |
E
esterzhou 已提交
27 28

- TextPickerResult
29
  | Name | Type | Description | 
E
esterzhou 已提交
30
  | -------- | -------- | -------- |
31 32
  | value | string | Text of the selected item. | 
  | index | number | Index value of the selected item in the range. | 
E
esterzhou 已提交
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

## Example

```
@Entry
@Component
struct TextPickerDialogExample {
  @State select: number = 1
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']

  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center,
      justifyContent: FlexAlign.Center }) {
      Button("TextPickerDialog").onClick(() => {
        TextPickerDialog.show({
          range: this.fruits,
          selected: this.select,
          onAccept: (value: TextPickerResult) => {
            console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
            this.select = value.index
          },
          onCancel: () => {
            console.info("TextPickerDialog:onCancel()")
          },
          onChange: (value: TextPickerResult) => {
            console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
          }
        })
      })
    }
  }
}
```