• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TextPicker
2
3滑动选择文本内容的组件。
4
5>  **说明:**
6>
7>  该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 子组件
11
1213
14
15## 接口
16
17TextPicker(options?: TextPickerOptions)
18
19根据range指定的选择范围创建文本选择器。
20
21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
22
23**系统能力:** SystemCapability.ArkUI.ArkUI.Full
24
25**参数:**
26
27| 参数名  | 类型                                            | 必填 | 说明                   |
28| ------- | ----------------------------------------------- | ---- | ---------------------- |
29| options | [TextPickerOptions](#textpickeroptions对象说明) | 否   | 配置文本选择器的参数。 |
30
31## TextPickerOptions对象说明
32
33**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
34
35**系统能力:** SystemCapability.ArkUI.ArkUI.Full
36
37| 名称 | 类型 | 必填 | 说明 |
38| -------- | -------- | -------- | -------- |
39| range | string[]&nbsp;\|&nbsp;string[] []<sup>10+</sup> \| [Resource](ts-types.md#resource类型)&nbsp;\|<br/>[TextPickerRangeContent](#textpickerrangecontent10对象说明)[]<sup>10+</sup>&nbsp;\|&nbsp;[TextCascadePickerRangeContent](#textcascadepickerrangecontent10对象说明)[]<sup>10+</sup> | 是 | 选择器的数据选择列表。不可设置为空数组,若设置为空数组,则不显示;若动态变化为空数组,则保持当前正常值显示。<br/>**说明**:单列数据选择器使用string[],Resource,TextPickerRangeContent[]类型。<br/>多列数据选择器使用string[][]类型。 <br/>多列联动数据选择器使用TextCascadePickerRangeContent[]类型。<br/>Resource类型只支持[strarray.json](../../../quick-start/resource-categories-and-access.md#资源组目录)。<br>range的类型及列数不可以动态修改。|
40| selected | number&nbsp;\|&nbsp;number[]<sup>10+</sup> | 否 | 设置默认选中项在数组中的索引值。<br/>默认值:0 <br/>**说明**:单列数据选择器使用number类型。<br/>多列、多列联动数据选择器使用number[]类型。<br />从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。|
41| value | string&nbsp;\|&nbsp;string[]<sup>10+</sup> | 否 | 设置默认选中项的值,优先级低于selected。<br/>默认值:第一个元素值<br/>**说明**:只有显示文本列表时该值有效。显示图片或图片加文本的列表时,该值无效。 <br/>单列数据选择器使用string类型。<br/>多列、多列联动数据选择器使用string[]类型。<br />从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。|
42
43## TextPickerRangeContent<sup>10+</sup>对象说明
44
45**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
46
47**系统能力:** SystemCapability.ArkUI.ArkUI.Full
48
49| 名称 | 类型                                                 | 必填 | 说明       |
50| ---- | ---------------------------------------------------- | ---- | ---------- |
51| icon | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 是   | 图片资源。 |
52| text | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 否   | 文本信息。 |
53
54## TextCascadePickerRangeContent<sup>10+</sup>对象说明
55
56**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
57
58**系统能力:** SystemCapability.ArkUI.ArkUI.Full
59
60| 名称 | 类型                                                 | 必填 | 说明   |
61| ------ | -------------------------------------------------------- | ---- | ---------- |
62| text   | string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | 是   | 文本信息。 |
63| children   | [TextCascadePickerRangeContent](#textcascadepickerrangecontent10对象说明)[] | 否   | 联动数据。 |
64## DividerOptions<sup>12+</sup>对象说明
65
66**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
67
68**系统能力:** SystemCapability.ArkUI.ArkUI.Full
69
70| 名称        | 类型                                 | 必填 | 说明                                                         |
71| ----------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
72| strokeWidth | [Dimension](ts-types.md#dimension10) | 否   | 分割线的线宽(默认单位vp),也可指定单位为px,不支持"百分比"类型。 |
73| startMargin | [Dimension](ts-types.md#dimension10) | 否   | 分割线与TextPicker侧边起始端的距离(默认单位vp),也可指定单位为px,不支持“百分比”类型。 |
74| endMargin   | [Dimension](ts-types.md#dimension10) | 否   | 分割线与TextPicker侧边结束端的距离(默认单位vp),也可指定单位为px,不支持“百分比”类型。 |
75| color       | [ResourceColor](ts-types.md#resourcecolor)  | 否   | 分割线的颜色。
76
77## 属性
78
79除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
80
81### defaultPickerItemHeight
82
83defaultPickerItemHeight(value: number | string)
84
85设置Picker各选择项的高度。
86
87**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
88
89**系统能力:** SystemCapability.ArkUI.ArkUI.Full
90
91**参数:**
92
93| 参数名 | 类型                       | 必填 | 说明                   |
94| ------ | -------------------------- | ---- | ---------------------- |
95| value  | number&nbsp;\|&nbsp;string | 是   | Picker各选择项的高度。 |
96
97### disappearTextStyle<sup>10+</sup>
98
99disappearTextStyle(value: PickerTextStyle)
100
101设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。
102
103**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
104
105**系统能力:** SystemCapability.ArkUI.ArkUI.Full
106
107**参数:**
108
109| 参数名 | 类型                                                         | 必填 | 说明                                                         |
110| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
111| value  | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 是   | 所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '14fp', <br/>weight: FontWeight.Regular<br/>}<br/>} |
112
113### textStyle<sup>10+</sup>
114
115textStyle(value: PickerTextStyle)
116
117设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。
118
119**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
120
121**系统能力:** SystemCapability.ArkUI.ArkUI.Full
122
123**参数:**
124
125| 参数名 | 类型                                                         | 必填 | 说明                                                         |
126| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
127| value  | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 是   | 所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '16fp', <br/>weight: FontWeight.Regular<br/>}<br/>} |
128
129### selectedTextStyle<sup>10+</sup>
130
131selectedTextStyle(value: PickerTextStyle)
132
133设置选中项的文本颜色、字号、字体粗细。
134
135**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
136
137**系统能力:** SystemCapability.ArkUI.ArkUI.Full
138
139**参数:**
140
141| 参数名 | 类型                                                         | 必填 | 说明                                                         |
142| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
143| value  | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 是   | 选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20vp', <br/>weight: FontWeight.Medium<br/>}<br/>} |
144
145### selectedIndex<sup>10+</sup>
146
147selectedIndex(value: number | number[])
148
149设置默认选中项在数组中的索引值,优先级高于options中的选中值。单列数据选择器使用number类型。多列、多列联动数据选择器使用number[]类型。
150
151**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
152
153**系统能力:** SystemCapability.ArkUI.ArkUI.Full
154
155**参数:**
156
157| 参数名 | 类型                         | 必填 | 说明                         |
158| ------ | ---------------------------- | ---- | ---------------------------- |
159| value  | number&nbsp;\|&nbsp;number[] | 是   | 默认选中项在数组中的索引值。<br/>默认值:0 <br/> |
160
161### canLoop<sup>10+</sup>
162
163canLoop(value: boolean)
164
165设置是否可循环滚动。
166
167**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
168
169**系统能力:** SystemCapability.ArkUI.ArkUI.Full
170
171**参数:**
172
173| 参数名 | 类型    | 必填 | 说明                                                         |
174| ------ | ------- | ---- | ------------------------------------------------------------ |
175| value  | boolean | 是   | 是否可循环滚动。<br/>true:可循环,false:不可循环。<br/>默认值:true |
176
177### divider<sup>12+</sup>
178
179divider(value: DividerOptions | null)
180
181设置分割线样式,不设置该属性则按“默认值”展示分割线。
182
183startMargin + endMargin 超过组件宽度后startMargin和endMargin会被置0。
184
185**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
186
187**系统能力:** SystemCapability.ArkUI.ArkUI.Full
188
189**参数:**
190| 参数名 | 类型    | 必填 | 说明                                                                  |
191| ------ | ------- | ---- | --------------------------------------------------------------------- |
192| value | [DividerOptions](#divideroptions12对象说明) \| null | 是   | 1.设置DividerOptions,则按设置的样式显示分割线。<br/>默认值:<br/>{<br/>strokeWidth: '2px', <br/>startMargin: 0, <br/>endMargin: 0, <br/>color: '#33000000'<br/>}<br/>2.设置为null不显示分割线。 |
193
194### gradientHeight<sup>12+</sup>
195
196gradientHeight(value: Dimension)
197
198设置渐隐效果高度,不设置该属性则显示默认渐隐效果。
199
200**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
201
202**系统能力:** SystemCapability.ArkUI.ArkUI.Full
203
204**参数:**
205
206| 参数名 | 类型    | 必填 | 说明                                                         |
207| ------ | ------- | ---- | ------------------------------------------------------------ |
208| value  | [Dimension](ts-types.md#dimension10) | 是   | 内容区上下边缘的渐隐高度(支持百分比,100%为TextPicker高度的一半即最大值),设置为0时不显示渐隐效果,负数等非法值显示默认渐隐效果。默认值为36vp。|
209
210> **说明:**
211>
212> 该组件不建议开发者在动效过程中修改属性数据。
213
214## 事件
215
216除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件:
217
218### onAccept<sup>(deprecated) </sup>
219
220onAccept(callback: (value: string, index: number) => void)
221
222点击弹窗中的“确定”按钮时触发该回调。该事件仅在[文本滑动选择器弹窗](ts-methods-textpicker-dialog.md)中生效。
223
224从API version 10开始废弃。
225
226**系统能力:** SystemCapability.ArkUI.ArkUI.Full
227
228**参数:**
229
230| 参数名 | 类型   | 必填 | 说明                 |
231| ------ | ------ | ---- | -------------------- |
232| value  | string | 是   | 当前选中项的文本。   |
233| index  | number | 是   | 当前选中项的索引值。 |
234
235### onCancel<sup>(deprecated) </sup>
236
237onCancel(callback: () => void)
238
239点击弹窗中的“取消”按钮时触发该回调。该事件仅在[文本滑动选择器弹窗](ts-methods-textpicker-dialog.md)中生效。
240
241从API version 10开始废弃。
242
243**系统能力:** SystemCapability.ArkUI.ArkUI.Full
244
245### onChange
246
247onChange(callback:&nbsp;(value:&nbsp;string&nbsp;\|&nbsp;string[],&nbsp;index:&nbsp;number&nbsp;\|&nbsp;number[])&nbsp;=&gt;&nbsp;void)
248
249滑动选中TextPicker文本内容后,触发该回调。当显示文本或图片加文本列表时,value值为选中项中的文本值,当显示图片列表时,value值为空。
250
251**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
252
253**系统能力:** SystemCapability.ArkUI.ArkUI.Full
254
255**参数:**
256
257| 参数名 | 类型                                       | 必填 | 说明                                              |
258| ------ | ------------------------------------------ | ---- | ------------------------------------------------- |
259| value  | string&nbsp;\|&nbsp;string[]<sup>10+</sup> | 是   | 当前选中项的文本。多列的情况,value为数组类型。   |
260| index  | number&nbsp;\|&nbsp;number[]<sup>10+</sup> | 是   | 当前选中项的索引值。多列的情况,index为数组类型。 |
261
262
263## 示例
264
265```ts
266// xxx.ets
267class bottom {
268  bottom:number = 50
269}
270let bott:bottom = new bottom()
271@Entry
272@Component
273struct TextPickerExample {
274  private select: number = 1
275  private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4']
276  private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4']
277  private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4']
278  private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits]
279  private cascade: TextCascadePickerRangeContent[] = [
280    {
281      text: '辽宁省',
282      children: [{ text: '沈阳市', children: [{ text: '沈河区' }, { text: '和平区' }, { text: '浑南区' }] },
283        { text: '大连市', children: [{ text: '中山区' }, { text: '金州区' }, { text: '长海县' }] }]
284    },
285    {
286      text: '吉林省',
287      children: [{ text: '长春市', children: [{ text: '南关区' }, { text: '宽城区' }, { text: '朝阳区' }] },
288        { text: '四平市', children: [{ text: '铁西区' }, { text: '铁东区' }, { text: '梨树县' }] }]
289    },
290    {
291      text: '黑龙江省',
292      children: [{ text: '哈尔滨市', children: [{ text: '道里区' }, { text: '道外区' }, { text: '南岗区' }] },
293        { text: '牡丹江市', children: [{ text: '东安区' }, { text: '西安区' }, { text: '爱民区' }] }]
294    }
295  ]
296
297  build() {
298    Column() {
299
300      TextPicker({ range: this.apfruits, selected: this.select })
301        .onChange((value: string | string[], index: number | number[]) => {
302          console.info('Picker item changed, value: ' + value + ', index: ' + index)
303        }).margin(bott)
304
305      TextPicker({ range: this.multi })
306        .onChange((value: string | string[], index: number | number[]) => {
307          console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
308        }).margin(bott)
309
310      TextPicker({ range: this.cascade })
311        .onChange((value: string | string[], index: number | number[]) => {
312          console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index))
313        })
314    }
315  }
316}
317```
318
319![textpicker](figures/textpicker.gif)
320
321```ts
322// xxx.ets
323@Entry
324@Component
325struct TextPickerExample {
326  private select: number = 1
327  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
328
329  build() {
330    Column() {
331      TextPicker({ range: this.fruits, selected: this.select })
332        .onChange((value: string | string[], index: number | number[]) => {
333          console.info('Picker item changed, value: ' + value + ', index: ' + index)
334        })
335        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
336        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
337        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
338    }.width('100%').height('100%')
339  }
340}
341```
342
343![textpicker](figures/textpicker1.gif)
344
345```ts
346// xxx.ets
347@Entry
348@Component
349struct TextPickerExample {
350  private select: number = 1
351  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
352
353  build() {
354    Column() {
355      TextPicker({ range: this.fruits, selected: this.select })
356        .onChange((value: string | string[], index: number | number[]) => {
357          console.info('Picker item changed, value: ' + value + ', index: ' + index)
358        })
359        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
360        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
361        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
362        .divider(null)
363    }.width('100%').height('100%')
364  }
365}
366```
367![textpicker](figures/textpicker2.gif)
368
369```ts
370// xxx.ets
371@Entry
372@Component
373struct TextPickerExample {
374  private select: number = 1
375  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
376
377  build() {
378    Column() {
379      TextPicker({ range: this.fruits, selected: this.select })
380        .onChange((value: string | string[], index: number | number[]) => {
381          console.info('Picker item changed, value: ' + value + ', index: ' + index)
382        })
383        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
384        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
385        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
386        .divider({
387          strokeWidth: 10,
388          color: Color.Red,
389          startMargin: 10,
390          endMargin: 20
391        } as DividerOptions)
392    }.width('100%').height('100%')
393  }
394}
395```
396![textpicker](figures/textpicker3.gif)
397```ts
398// xxx.ets 该示例实现了通过gradientHeight()自定义TextPicker的渐隐效果高度
399@Entry
400@Component
401struct TextPickerExample {
402  private select: number = 1
403  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
404
405  build() {
406    Column() {
407      TextPicker({ range: this.fruits, selected: this.select })
408        .onChange((value: string | string[], index: number | number[]) => {
409          console.info('Picker item changed, value: ' + value + ', index: ' + index)
410        })
411        .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}})
412        .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}})
413        .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}})
414        .gradientHeight(100)
415    }.width('100%').height('100%')
416  }
417}
418```
419
420![textpicker](figures/textpicker4.gif)