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

E
ester.zhou 已提交
3
A text picker dialog box is a dialog box that allows users to select text from the given range.
E
esterzhou 已提交
4

E
ester.zhou 已提交
5 6
>  **NOTE**
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8 9


E
esterzhou 已提交
10 11 12 13 14 15
## Required Permissions

None

## TextPickerDialog.show

E
ester.zhou 已提交
16
show(options?: TextPickerDialogOptions)
E
esterzhou 已提交
17 18 19

Shows a text picker in the given settings.

E
ester.zhou 已提交
20
- TextPickerDialogOptions
E
ester.zhou 已提交
21
  | Name| Type| Mandatory| Default Value| Description|
E
esterzhou 已提交
22
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
23
  | range | string[] \| [Resource](../../ui/ts-types.md#resource)| Yes| - | Data selection range of the picker.|
E
ester.zhou 已提交
24 25
  | selected | number | No| 0 | Index of the selected item in the range.|
  | value       | string           | No   | -    | Value of the selected item. This parameter does not take effect when the **selected** parameter is set. If the value is not within the range, the first item in the range is used instead.|
E
ester.zhou 已提交
26
  | defaultPickerItemHeight | number \| string | No| - | Default height of an item in the picker.|
E
ester.zhou 已提交
27 28 29
  | onAccept | (value: TextPickerResult) => void | No| - | Callback invoked when the OK button in the dialog box is clicked.|
  | onCancel | () => void | No| - | Callback invoked 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 已提交
30 31

- TextPickerResult
E
ester.zhou 已提交
32
  | Name| Type| Description|
E
esterzhou 已提交
33
  | -------- | -------- | -------- |
E
ester.zhou 已提交
34 35
  | value | string | Value of the selected item.|
  | index | number | Index of the selected item in the range.|
E
esterzhou 已提交
36 37 38

## Example

E
ester.zhou 已提交
39 40
```ts
// xxx.ets
E
esterzhou 已提交
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
@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))
          }
        })
      })
    }
  }
}
```