ts-methods-textpicker-dialog.md 3.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
esterzhou 已提交
7 8
> 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 已提交
9

E
esterzhou 已提交
10 11
## TextPickerDialog.show

E
ester.zhou 已提交
12
show(options?: TextPickerDialogOptions)
E
esterzhou 已提交
13 14 15

Shows a text picker in the given settings.

E
esterzhou 已提交
16 17 18 19
**TextPickerDialogOptions**

| Name| Type| Mandatory|  Description|
| -------- | -------- | -------- |  -------- |
E
ester.zhou 已提交
20
| range | string[] \| [Resource](ts-types.md#resource)\|[TextPickerRangeContent](ts-basic-components-textpicker.md#textpickerrangecontent10)[]<sup>10+</sup> | 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.|
E
esterzhou 已提交
21 22 23
| selected | number | No|  Index of the selected item.<br>Default value: **0**|
| value       | string           | No   | Text 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.|
| defaultPickerItemHeight | number \| string | No| Height of the picker item.|
E
ester.zhou 已提交
24 25 26
| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width for the top and bottom items.|
| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of all items except the top, bottom, and selected items.|
| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10) | No| Font color, font size, and font width of the selected item.|
E
esterzhou 已提交
27 28 29 30 31
| onAccept | (value: [TextPickerResult](#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](#textpickerresult)) => void | No|  Callback invoked when the selected item changes.|

## TextPickerResult
E
esterzhou 已提交
32

E
esterzhou 已提交
33 34
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
35
| value | string | Text of the selected item.<br>**NOTE**<br>For a text list or a list consisting of text and images, **value** indicates the text value of the selected item.<br>For an image list, **value** is empty.|
E
esterzhou 已提交
36
| index | number | Index of the selected item in the range.|
E
esterzhou 已提交
37 38 39

## Example

E
ester.zhou 已提交
40 41
```ts
// xxx.ets
E
esterzhou 已提交
42 43 44
@Entry
@Component
struct TextPickerDialogExample {
E
esterzhou 已提交
45 46
  @State select: number = 2
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
E
esterzhou 已提交
47 48

  build() {
E
esterzhou 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    Column() {
      Button("TextPickerDialog")
        .margin(20)
        .onClick(() => {
          TextPickerDialog.show({
            range: this.fruits,
            selected: this.select,
            onAccept: (value: TextPickerResult) => {
              // Set select to the index of the item selected when the OK button is touched. In this way, when the text picker dialog box is displayed again, the selected item is the one last confirmed.
              this.select = value.index
              console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
            },
            onCancel: () => {
              console.info("TextPickerDialog:onCancel()")
            },
            onChange: (value: TextPickerResult) => {
              console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
            }
          })
E
esterzhou 已提交
68
        })
E
esterzhou 已提交
69
    }.width('100%')
E
esterzhou 已提交
70 71 72
  }
}
```