1# AlphabetIndexer 2 3可以与容器组件联动用于按逻辑结构快速定位容器显示区域的组件。 4 5> **说明:** 6> 7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12无 13 14 15## 接口 16 17AlphabetIndexer(value: {arrayValue: Array<string>, selected: number}) 18 19**参数:** 20 21| 参数名 | 参数类型 | 必填 | 参数描述 | 22| -------- | -------- | -------- | -------- | 23| arrayValue | Array<string> | 是 | 字母索引字符串数组,不可设置为空。 | 24| selected | number | 是 | 初始选中项索引值,若超出索引值范围,则取默认值0。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 25 26## 属性 27 28除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 29 30| 名称 | 参数类型 | 描述 | 31| ----------------------- | --------------------| ------------------------------------------------------------------| 32| color | [ResourceColor](ts-types.md#resourcecolor) | 设置文字颜色。<br/>默认值:0x99000000。 | 33| selectedColor | [ResourceColor](ts-types.md#resourcecolor) | 设置选中项文字颜色。<br/>默认值:0xFF254FF7。 | 34| popupColor | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗文字颜色。<br/>默认值:0xFF254FF7。 | 35| selectedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | 设置选中项背景颜色。<br/>默认值:0x1F0A59F7。 | 36| popupBackground | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗背景色。<br/>默认值:0xFFFFFFFF。 | 37| usingPopup | boolean | 设置是否使用提示弹窗。<br/>默认值:false。 | 38| selectedFont | [Font](ts-types.md#font) | 设置选中项文字样式。<br/>默认值:<br/>{<br/>size:'12.0fp',<br/> style:FontStyle.Normal,<br/> weight:FontWeight.Normal,<br/> family:'HarmonyOS Sans'<br/>} | 39| popupFont | [Font](ts-types.md#font) | 设置提示弹窗字体样式。<br/>默认值:<br/>{<br/>size:'24.0vp',<br/> style:FontStyle.Normal,<br/> weight:FontWeight.Normal,<br/> family:'HarmonyOS Sans'<br/>} | 40| font | [Font](ts-types.md#font) | 设置字母索引条默认字体样式。<br/>默认值:<br/>{<br/>size:'12.0fp',<br/> style:FontStyle.Normal,<br/> weight:FontWeight.Normal,<br/> family:'HarmonyOS Sans'<br/>} | 41| itemSize | string \| number | 设置字母索引条字母区域大小,字母区域为正方形,即正方形边长。不支持设置为百分比。<br/>默认值:16.0<br/>单位:vp | 42| alignStyle | value: [IndexerAlign](#indexeralign枚举说明),<br/>offset<sup>10+</sup>?: [Length](ts-types.md#length) | value:设置字母索引条弹框的对齐样式,支持弹窗显示在索引条右侧和左侧。<br/>默认值: IndexerAlign.Right。<br/>offset:设置提示弹窗与索引条之间间距,大于等于0为有效值,在不设置或设置为小于0的情况下间距与popupPosition.x相同。与popupPosition同时设置时,水平方向上offset生效,竖直方向上popupPosition.y生效。 | 43| selected | number | 设置选中项索引值。<br/>默认值:0。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 44| popupPosition | [Position](ts-types.md#position8) | 设置弹出窗口相对于索引器条上边框中点的位置。<br/>默认值:{x:60.0, y:48.0}。 | 45| popupSelectedColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗非字母部分选中文字色。 <br/>默认值:#FF182431 | 46| popupUnselectedColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗非字母部分未选中文字色。 <br/>默认值:#FF182431 | 47| popupItemFont<sup>10+</sup> | [Font](ts-types.md#font) | 设置提示弹窗非字母部分字体样式。 <br/>默认值:<br/>{<br/>size:24,<br/>style:FontStyle.Medium<br/>}| 48| popupItemBackgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗非字母部分背景色。 <br/>默认值:#FFFFFF | 49 50## IndexerAlign枚举说明 51 52| 名称 | 描述 | 53| -------- | -------- | 54| Left | 弹框显示在索引条右侧。 | 55| Right | 弹框显示在索引条左侧。 | 56 57## 事件 58 59除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 60 61| 名称 | 功能描述 | 62| -------- | -------- | 63| onSelected(callback: (index: number) => void)<sup>(deprecated)</sup> | 索引条选中回调,返回值为当前选中索引。 从API Version 8开始废弃,建议使用onSelect代替。 | 64| onSelect(callback: (index: number) => void)<sup>8+</sup> | 索引条选中回调,返回值为当前选中索引。 | 65| onRequestPopupData(callback: (index: number) => Array<string>)<sup>8+</sup> | 选中字母索引后,请求索引提示弹窗显示内容回调。<br/>返回值:索引对应的字符串数组,此字符串数组在弹窗中竖排显示,字符串列表最多显示5个,超出部分可以滑动显示。 | 66| onPopupSelect(callback: (index: number) => void)<sup>8+</sup> | 字母索引提示弹窗字符串列表选中回调。 | 67 68 69## 示例 70 71```ts 72// xxx.ets 73@Entry 74@Component 75struct AlphabetIndexerSample { 76 private arrayA: string[] = ['安'] 77 private arrayB: string[] = ['卜', '白', '包', '毕', '丙'] 78 private arrayC: string[] = ['曹', '成', '陈', '催'] 79 private arrayL: string[] = ['刘', '李', '楼', '梁', '雷', '吕', '柳', '卢'] 80 private value: string[] = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 81 'H', 'I', 'J', 'K', 'L', 'M', 'N', 82 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 83 'V', 'W', 'X', 'Y', 'Z'] 84 85 build() { 86 Stack({ alignContent: Alignment.Start }) { 87 Row() { 88 List({ space: 20, initialIndex: 0 }) { 89 ForEach(this.arrayA, (item: string) => { 90 ListItem() { 91 Text(item) 92 .width('80%') 93 .height('5%') 94 .fontSize(30) 95 .textAlign(TextAlign.Center) 96 }.editable(true) 97 }, (item: string) => item) 98 99 ForEach(this.arrayB, (item: string) => { 100 ListItem() { 101 Text(item) 102 .width('80%') 103 .height('5%') 104 .fontSize(30) 105 .textAlign(TextAlign.Center) 106 }.editable(true) 107 }, (item: string) => item) 108 109 ForEach(this.arrayC, (item: string) => { 110 ListItem() { 111 Text(item) 112 .width('80%') 113 .height('5%') 114 .fontSize(30) 115 .textAlign(TextAlign.Center) 116 }.editable(true) 117 }, (item: string) => item) 118 119 ForEach(this.arrayL, (item: string) => { 120 ListItem() { 121 Text(item) 122 .width('80%') 123 .height('5%') 124 .fontSize(30) 125 .textAlign(TextAlign.Center) 126 }.editable(true) 127 }, (item: string) => item) 128 } 129 .width('50%') 130 .height('100%') 131 132 AlphabetIndexer({ arrayValue: this.value, selected: 0 }) 133 .selectedColor(0xFFFFFF) // 选中项文本颜色 134 .popupColor(0xFFFAF0) // 弹出框文本颜色 135 .selectedBackgroundColor(0xCCCCCC) // 选中项背景颜色 136 .popupBackground(0xD2B48C) // 弹出框背景颜色 137 .usingPopup(true) // 是否显示弹出框 138 .selectedFont({ size: 16, weight: FontWeight.Bolder }) // 选中项字体样式 139 .popupFont({ size: 30, weight: FontWeight.Bolder }) // 弹出框内容的字体样式 140 .itemSize(28) // 每一项的尺寸大小 141 .alignStyle(IndexerAlign.Left) // 弹出框在索引条右侧弹出 142 .popupSelectedColor(0x00FF00) 143 .popupUnselectedColor(0x0000FF) 144 .popupItemFont({ size: 30, style: FontStyle.Normal }) 145 .popupItemBackgroundColor(0xCCCCCC) 146 .onSelect((index: number) => { 147 console.info(this.value[index] + ' Selected!') 148 }) 149 .onRequestPopupData((index: number) => { 150 if (this.value[index] == 'A') { 151 return this.arrayA // 当选中A时,弹出框里面的提示文本列表显示A对应的列表arrayA,选中B、C、L时也同样 152 } else if (this.value[index] == 'B') { 153 return this.arrayB 154 } else if (this.value[index] == 'C') { 155 return this.arrayC 156 } else if (this.value[index] == 'L') { 157 return this.arrayL 158 } else { 159 return [] // 选中其余子母项时,提示文本列表为空 160 } 161 }) 162 .onPopupSelect((index: number) => { 163 console.info('onPopupSelected:' + index) 164 }) 165 } 166 .width('100%') 167 .height('100%') 168 } 169 } 170} 171``` 172 173 174