ts-methods-textpicker-dialog.md 2.7 KB
Newer Older
1 2
# 文本滑动选择器弹窗

3 4
根据指定的选择范围创建文本选择器,展示在弹窗上。

H
geshi  
HelloCrease 已提交
5
>  **说明:**
6
>
7 8 9 10 11
> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。


## TextPickerDialog.show

L
fix doc  
luoying_ace_admin 已提交
12
show(options?: TextPickerDialogOptions)
13 14 15

定义文本滑动选择器弹窗并弹出。

L
fix doc  
luoying_ace_admin 已提交
16
- TextPickerDialogOptions参数说明
17

18 19 20 21 22 23 24 25 26
  | 参数名 | 参数类型 | 必填 |  参数描述 |
  | -------- | -------- | -------- |  -------- |
  | range | string[] \| [Resource](../../ui/ts-types.md#resource类型) | 是 |  设置文本选择器的选择范围。 |
  | selected | number | 否 |  设置选中项的索引值。<br>默认值:0 |
  | value       | string           | 否    | 设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认取range第一个元素。|
  | defaultPickerItemHeight | number \| string | 否 | 设置选择器中选项的高度。 |
  | onAccept | (value: TextPickerResult) => void | 否 |  点击弹窗中的“确定”按钮时触发该回调。 |
  | onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。 |
  | onChange | (value: TextPickerResult) => void | 否 |  滑动弹窗中的选择器使当前选中项改变时触发该回调。 |
27 28

- TextPickerResult对象说明
29

30
  | 名称 | 类型 | 描述 | 
31
  | -------- | -------- | -------- |
32 33
  | value | string | 选中项的文本内容。 | 
  | index | number | 选中项在选择范围数组中的索引值。 | 
34 35 36

## 示例

H
geshi  
HelloCrease 已提交
37 38
```ts
// xxx.ets
39 40 41
@Entry
@Component
struct TextPickerDialogExample {
42 43
  @State select: number = 2
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
44 45

  build() {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    Column() {
      Button("TextPickerDialog")
        .margin(20)
        .onClick(() => {
          TextPickerDialog.show({
            range: this.fruits,
            selected: this.select,
            onAccept: (value: TextPickerResult) => {
              // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
              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))
            }
          })
65
        })
66
    }.width('100%')
67 68 69
  }
}
```