1# TextPicker 2 3滑动选择文本内容的组件。 4 5> **说明:** 6> 7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12无 13 14 15## 接口 16 17TextPicker(options?: {range: string[] | string[][] | Resource | TextPickerRangeContent[] | TextCascadePickerRangeContent[], selected?: number, value?: string}) 18 19根据range指定的选择范围创建文本选择器。 20 21**参数:** 22 23| 参数名 | 参数类型 | 必填 | 参数描述 | 24| -------- | -------- | -------- | -------- | 25| 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#资源组目录)。 | 26| selected | number \| number[]<sup>10+</sup> | 否 | 设置默认选中项在数组中的索引值。<br/>默认值:0 <br/>**说明**:单列数据选择器使用number类型。<br/>多列、多列联动数据选择器使用number[]类型。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。| 27| value | string \| string[]<sup>10+</sup> | 否 | 设置默认选中项的值,优先级低于selected。<br/>默认值:第一个元素值<br/>**说明**:只有显示文本列表时该值有效。显示图片或图片加文本的列表时,该值无效。 <br/>单列数据选择器使用string类型。<br/>多列、多列联动数据选择器使用string[]类型。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。| 28 29## TextPickerRangeContent<sup>10+</sup>类型说明 30 31| 参数名 | 参数类型 | 必填 | 参数描述 | 32| ------ | -------------------------------------------------------- | ---- | ---------- | 33| icon | string \| [Resource](ts-types.md#resource) | 是 | 图片资源。 | 34| text | string \| [Resource](ts-types.md#resource) | 否 | 文本信息。 | 35 36## TextCascadePickerRangeContent<sup>10+</sup>类型说明 37 38| 参数名 | 参数类型 | 必填 | 参数描述 | 39| ------ | -------------------------------------------------------- | ---- | ---------- | 40| text | string \| [Resource](ts-types.md#resource) | 是 | 文本信息。 | 41| children | [TextCascadePickerRangeContent](#textcascadepickerrangecontent10类型说明)[] | 否 | 联动数据。 | 42 43## 属性 44 45除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 46 47| 名称 | 参数类型 | 描述 | 48| -------- | -------- | -------- | 49| defaultPickerItemHeight | number \| string | 设置Picker各选择项的高度。 | 50| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br>size: '14fp', <br/>weight: FontWeight.Regular<br/>}<br/>} | 51| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '16fp', <br/>weight: FontWeight.Regular<br/>}<br/>} | 52| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 设置选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20vp', <br/>weight: FontWeight.Medium<br/>}<br/>} | 53| selectedIndex<sup>10+</sup> | number \| number[] | 设置默认选中项在数组中的索引值,优先级高于options中的选中值。 <br/>**说明**:单列数据选择器使用number类型。多列、多列联动数据选择器使用number[]类型。 | 54| canLoop<sup>10+</sup> | boolean | 设置是否可循环滚动,true:可循环,false:不可循环,默认值:true。 | 55 56## 事件 57 58除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 59 60| 名称 | 描述 | 61| -------- | -------- | 62| onAccept(callback: (value: string, index: number) => void)<sup>(deprecated) </sup> | 点击弹窗中的“确定”按钮时触发该回调。<br/>- value: 当前选中项的文本。<br/>- index: 当前选中项的索引值。<br/> 从API version 10开始废弃。 <br/>**说明:** <br/>该事件仅在[文本滑动选择器弹窗](ts-methods-textpicker-dialog.md)中生效。| 63| onCancel(callback: () => void)<sup>(deprecated) </sup> | 点击弹窗中的“取消”按钮时触发该回调。<br/> 从API version 10开始废弃。<br/>**说明:** <br/>该事件仅在[文本滑动选择器弹窗](ts-methods-textpicker-dialog.md)中生效。 | 64| onChange(callback: (value: string \| string[]<sup>10+</sup>, index: number \| number[]<sup>10+</sup>) => void) | 滑动选中TextPicker文本内容后,触发该回调。<br/>- value: 当前选中项的文本。(多列的情况,value为数组类型。)<br/>- index: 当前选中项的索引值。(多列的情况,index为数组类型。)<br/>**说明**:当显示文本或图片加文本列表时,value值为选中项中的文本值,当显示图片列表时,value值为空。 | 65 66 67## 示例 68 69```ts 70// xxx.ets 71class bottom { 72 bottom:number = 50 73} 74let bott:bottom = new bottom() 75@Entry 76@Component 77struct TextPickerExample { 78 private select: number = 1 79 private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4'] 80 private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4'] 81 private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4'] 82 private multi: string[][] = [this.apfruits, this.orfruits, this.pefruits] 83 private cascade: TextCascadePickerRangeContent[] = [ 84 { 85 text: '辽宁省', 86 children: [{ text: '沈阳市', children: [{ text: '沈河区' }, { text: '和平区' }, { text: '浑南区' }] }, 87 { text: '大连市', children: [{ text: '中山区' }, { text: '金州区' }, { text: '长海县' }] }] 88 }, 89 { 90 text: '吉林省', 91 children: [{ text: '长春市', children: [{ text: '南关区' }, { text: '宽城区' }, { text: '朝阳区' }] }, 92 { text: '四平市', children: [{ text: '铁西区' }, { text: '铁东区' }, { text: '梨树县' }] }] 93 }, 94 { 95 text: '黑龙江省', 96 children: [{ text: '哈尔滨市', children: [{ text: '道里区' }, { text: '道外区' }, { text: '南岗区' }] }, 97 { text: '牡丹江市', children: [{ text: '东安区' }, { text: '西安区' }, { text: '爱民区' }] }] 98 } 99 ] 100 101 build() { 102 Column() { 103 104 TextPicker({ range: this.apfruits, selected: this.select }) 105 .onChange((value: string | string[], index: number | number[]) => { 106 console.info('Picker item changed, value: ' + value + ', index: ' + index) 107 }).margin(bott) 108 109 TextPicker({ range: this.multi }) 110 .onChange((value: string | string[], index: number | number[]) => { 111 console.info('TextPicker 多列:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 112 }).margin(bott) 113 114 TextPicker({ range: this.cascade }) 115 .onChange((value: string | string[], index: number | number[]) => { 116 console.info('TextPicker 多列联动:onChange ' + JSON.stringify(value) + ', ' + 'index: ' + JSON.stringify(index)) 117 }) 118 } 119 } 120} 121``` 122 123 124 125```ts 126// xxx.ets 127@Entry 128@Component 129struct TextPickerExample { 130 private select: number = 1 131 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4'] 132 133 build() { 134 Column() { 135 TextPicker({ range: this.fruits, selected: this.select }) 136 .onChange((value: string | string[], index: number | number[]) => { 137 console.info('Picker item changed, value: ' + value + ', index: ' + index) 138 }) 139 .disappearTextStyle({color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}) 140 .textStyle({color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}) 141 .selectedTextStyle({color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}) 142 }.width('100%').height('100%') 143 } 144} 145``` 146 147 148