1# TextPicker 2 3滑动选择文本内容的组件。 4 5> **说明:** 6> 7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12无 13 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[] \| string[] []<sup>10+</sup> \| [Resource](ts-types.md#resource类型) \|<br/>[TextPickerRangeContent](#textpickerrangecontent10对象说明)[]<sup>10+</sup> \| [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 \| number[]<sup>10+</sup> | 否 | 设置默认选中项在数组中的索引值。<br/>默认值:0 <br/>**说明**:单列数据选择器使用number类型。<br/>多列、多列联动数据选择器使用number[]类型。<br />从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。| 41| value | string \| 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 \| [Resource](ts-types.md#resource) | 是 | 图片资源。 | 52| text | string \| [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 \| [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 \| 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 \| 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: (value: string \| string[], index: number \| number[]) => 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 \| string[]<sup>10+</sup> | 是 | 当前选中项的文本。多列的情况,value为数组类型。 | 260| index | number \| number[]<sup>10+</sup> | 是 | 当前选中项的索引值。多列的情况,index为数组类型。 | 261 262### onScrollStop<sup>14+</sup> 263 264onScrollStop(callback: TextPickerScrollStopCallback) 265 266文本选择器的选项列滑动停止时触发该事件。 267 268手指拖动选项列触发的滑动,手指离开屏幕且滑动停止时会触发该事件。 269 270当显示文本或图片加文本列表时,value值为选中项中的文本值,当显示图片列表时,value值为空。 271 272**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 273 274**系统能力:** SystemCapability.ArkUI.ArkUI.Full 275 276**参数:** 277 278| 参数名 | 类型 | 必填 | 说明 | 279| ------ | ------------------------------------------ | ---- | ------------------------------------------------- | 280| callback | [TextPickerScrollStopCallback](#textpickerscrollstopcallback14) | 是 | 文本选择器的选项列滑动停止时触发该事件。 | 281 282## TextPickerScrollStopCallback<sup>14+</sup> 283 284type TextPickerScrollStopCallback = (value: string | string[], index: number | number[]) => void 285 286文本选择器的选项列滑动停止时触发该事件。 287 288**卡片能力:** 从API version 14开始,该接口支持在ArkTS卡片中使用。 289 290**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 291 292**系统能力:** SystemCapability.ArkUI.ArkUI.Full 293 294**参数:** 295 296| 参数名 | 类型 | 必填 | 说明 | 297| ------ | ------------------------------------------ | ---- | ------------------------------------------------- | 298| value | string \| string[] | 是 | 当前选中项的文本。多列的情况,value为数组类型。 | 299| index | number \| number[] | 是 | 当前选中项的索引值。多列的情况,index为数组类型。 | 300 301## 示例 302 303### 示例1(设置选择器列数) 304 305该示例通过配置range实现单列或多列文本选择器。 306 307```ts 308// xxx.ets 309class bottom { 310 bottom:number = 50 311} 312let bott:bottom = new bottom() 313@Entry 314@Component 315struct TextPickerExample { 316 private select: number = 1 317 private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4'] 318 private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4'] 319 private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4'] 320 private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits] 321 private cascade: TextCascadePickerRangeContent[] = [ 322 { 323 text: '辽宁省', 324 children: [{ text: '沈阳市', children: [{ text: '沈河区' }, { text: '和平区' }, { text: '浑南区' }] }, 325 { text: '大连市', children: [{ text: '中山区' }, { text: '金州区' }, { text: '长海县' }] }] 326 }, 327 { 328 text: '吉林省', 329 children: [{ text: '长春市', children: [{ text: '南关区' }, { text: '宽城区' }, { text: '朝阳区' }] }, 330 { text: '四平市', children: [{ text: '铁西区' }, { text: '铁东区' }, { text: '梨树县' }] }] 331 }, 332 { 333 text: '黑龙江省', 334 children: [{ text: '哈尔滨市', children: [{ text: '道里区' }, { text: '道外区' }, { text: '南岗区' }] }, 335 { text: '牡丹江市', children: [{ text: '东安区' }, { text: '西安区' }, { text: '爱民区' }] }] 336 } 337 ] 338 339 build() { 340 Column() { 341 342 TextPicker({ range: this.apfruits, selected: this.select }) 343 .onChange((value: string | string[], index: number | number[]) => { 344 console.info('Picker item changed, value: ' + value + ', index: ' + index) 345 }) 346 .onScrollStop((value: string | string[], index: number | number[]) => { 347 console.info('Picker scroll stopped, value: ' + value + ', index: ' + index) 348 }).margin(bott) 349 350 TextPicker({ range: this.multi }) 351 .onChange((value: string | string[], index: number | number[]) => { 352 console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 353 }) 354 .onScrollStop((value: string | string[], index: number | number[]) => { 355 console.info('TextPicker 多列:onScrollStop ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 356 }).margin(bott) 357 358 TextPicker({ range: this.cascade }) 359 .onChange((value: string | string[], index: number | number[]) => { 360 console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 361 }) 362 .onScrollStop((value: string | string[], index: number | number[]) => { 363 console.info('TextPicker 多列联动:onScrollStop ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 364 }) 365 } 366 } 367} 368``` 369 370 371 372### 示例2(设置文本样式) 373 374该示例通过配置disappearTextStyle、textStyle、selectedTextStyle实现文本选择器中的文本样式。 375 376```ts 377// xxx.ets 378@Entry 379@Component 380struct TextPickerExample { 381 private select: number = 1 382 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4'] 383 384 build() { 385 Column() { 386 TextPicker({ range: this.fruits, selected: this.select }) 387 .onChange((value: string | string[], index: number | number[]) => { 388 console.info('Picker item changed, value: ' + value + ', index: ' + index) 389 }) 390 .onScrollStop((value: string | string[], index: number | number[]) => { 391 console.info('Picker scroll stopped, value: ' + value + ', index: ' + index) 392 }) 393 .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}) 394 .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}) 395 .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}) 396 }.width('100%').height('100%') 397 } 398} 399``` 400 401 402 403### 示例3(设置无分割线样式) 404 405该示例通过配置divider为null实现无分割线样式的文本选择器。 406 407```ts 408// xxx.ets 409@Entry 410@Component 411struct TextPickerExample { 412 private select: number = 1 413 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4'] 414 415 build() { 416 Column() { 417 TextPicker({ range: this.fruits, selected: this.select }) 418 .onChange((value: string | string[], index: number | number[]) => { 419 console.info('Picker item changed, value: ' + value + ', index: ' + index) 420 }) 421 .onScrollStop((value: string | string[], index: number | number[]) => { 422 console.info('Picker scroll stopped, value: ' + value + ', index: ' + index) 423 }) 424 .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}) 425 .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}) 426 .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}) 427 .divider(null) 428 }.width('100%').height('100%') 429 } 430} 431``` 432 433 434### 示例4(设置分割线样式) 435 436该示例通过配置divider的DividerOptions类型实现分割线样式的文本选择器。 437 438```ts 439// xxx.ets 440@Entry 441@Component 442struct TextPickerExample { 443 private select: number = 1 444 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4'] 445 446 build() { 447 Column() { 448 TextPicker({ range: this.fruits, selected: this.select }) 449 .onChange((value: string | string[], index: number | number[]) => { 450 console.info('Picker item changed, value: ' + value + ', index: ' + index) 451 }) 452 .onScrollStop((value: string | string[], index: number | number[]) => { 453 console.info('Picker scroll stopped, value: ' + value + ', index: ' + index) 454 }) 455 .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}) 456 .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}) 457 .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}) 458 .divider({ 459 strokeWidth: 10, 460 color: Color.Red, 461 startMargin: 10, 462 endMargin: 20 463 } as DividerOptions) 464 }.width('100%').height('100%') 465 } 466} 467``` 468 469### 示例5(设置渐隐效果) 470 471该示例通过gradientHeight自定义TextPicker的渐隐效果高度。 472 473```ts 474// xxx.ets 475@Entry 476@Component 477struct TextPickerExample { 478 private select: number = 1 479 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4'] 480 481 build() { 482 Column() { 483 TextPicker({ range: this.fruits, selected: this.select }) 484 .onChange((value: string | string[], index: number | number[]) => { 485 console.info('Picker item changed, value: ' + value + ', index: ' + index) 486 }) 487 .onScrollStop((value: string | string[], index: number | number[]) => { 488 console.info('Picker scroll stopped, value: ' + value + ', index: ' + index) 489 }) 490 .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}) 491 .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}) 492 .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}) 493 .gradientHeight(100) 494 }.width('100%').height('100%') 495 } 496} 497``` 498 499 500