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/>默认值:0x99182431。 | 33| selectedColor | [ResourceColor](ts-types.md#resourcecolor) | 设置选中项文字颜色。<br/>默认值:0xFF007DFF。 | 34| popupColor | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗文字颜色。<br/>默认值:0xFF007DFF。 | 35| selectedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | 设置选中项背景颜色。<br/>默认值:0x1A007DFF。 | 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<sup>8+</sup> | number | 设置选中项索引值。<br/>默认值:0。<br />从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 44| popupPosition<sup>8+</sup> | [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/>weight:FontWeight.Medium<br/>}| 48| popupItemBackgroundColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 设置提示弹窗非字母部分背景色。 <br/>默认值:#FFFFFFFF | 49| autoCollapse<sup>11+</sup> | boolean | 设置是否使用自适应折叠模式。<br/>默认值:false。<br/>**说明:**<br />- 如果字符串首字符为“#”,除去首字符。<br/>当剩余字符数 $\leq$ 9时,选择全显示模式。<br/>当9 < 剩余字符数 $\leq$ 13时,根据索引条高度自适应选择全显示模式或者短折叠模式。<br/>当剩余字符数 > 13时,根据索引条高度自适应选择短折叠模式或者长折叠模式。<br />- 如果字符串首字符不为“#”。<br/>当所有字符数 $\leq$ 9时,选择全显示模式。 <br/>当9 < 所有字符数 $\leq$ 13时,根据索引条高度自适应选择全显示模式或者短折叠模式。<br/>当所有字符数 > 13时,根据索引条高度自适应选择短折叠模式或者长折叠模式。 50 51## IndexerAlign枚举说明 52 53| 名称 | 描述 | 54| -------- | -------- | 55| Left | 弹框显示在索引条右侧。 | 56| Right | 弹框显示在索引条左侧。 | 57 58## 事件 59 60除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 61 62| 名称 | 功能描述 | 63| -------- | -------- | 64| onSelected(callback: (index: number) => void)<sup>(deprecated)</sup> | 索引条选中回调,返回值为当前选中索引。 从API Version 8开始废弃,建议使用onSelect代替。 | 65| onSelect(callback: (index: number) => void)<sup>8+</sup> | 索引条选中回调,返回值为当前选中索引。 | 66| onRequestPopupData(callback: (index: number) => Array<string>)<sup>8+</sup> | 选中字母索引后,请求索引提示弹窗显示内容回调。<br/>返回值:索引对应的字符串数组,此字符串数组在弹窗中竖排显示,字符串列表最多显示5个,超出部分可以滑动显示。 | 67| onPopupSelect(callback: (index: number) => void)<sup>8+</sup> | 字母索引提示弹窗字符串列表选中回调。 | 68 69 70## 示例 71### 示例1 72 73```ts 74// xxx.ets 75@Entry 76@Component 77struct AlphabetIndexerSample { 78 private arrayA: string[] = ['安'] 79 private arrayB: string[] = ['卜', '白', '包', '毕', '丙'] 80 private arrayC: string[] = ['曹', '成', '陈', '催'] 81 private arrayL: string[] = ['刘', '李', '楼', '梁', '雷', '吕', '柳', '卢'] 82 private value: string[] = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 83 'H', 'I', 'J', 'K', 'L', 'M', 'N', 84 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 85 'V', 'W', 'X', 'Y', 'Z'] 86 87 build() { 88 Stack({ alignContent: Alignment.Start }) { 89 Row() { 90 List({ space: 20, initialIndex: 0 }) { 91 ForEach(this.arrayA, (item: string) => { 92 ListItem() { 93 Text(item) 94 .width('80%') 95 .height('5%') 96 .fontSize(30) 97 .textAlign(TextAlign.Center) 98 } 99 }, (item: string) => item) 100 101 ForEach(this.arrayB, (item: string) => { 102 ListItem() { 103 Text(item) 104 .width('80%') 105 .height('5%') 106 .fontSize(30) 107 .textAlign(TextAlign.Center) 108 } 109 }, (item: string) => item) 110 111 ForEach(this.arrayC, (item: string) => { 112 ListItem() { 113 Text(item) 114 .width('80%') 115 .height('5%') 116 .fontSize(30) 117 .textAlign(TextAlign.Center) 118 } 119 }, (item: string) => item) 120 121 ForEach(this.arrayL, (item: string) => { 122 ListItem() { 123 Text(item) 124 .width('80%') 125 .height('5%') 126 .fontSize(30) 127 .textAlign(TextAlign.Center) 128 } 129 }, (item: string) => item) 130 } 131 .width('50%') 132 .height('100%') 133 134 AlphabetIndexer({ arrayValue: this.value, selected: 0 }) 135 .selectedColor(0xFFFFFF) // 选中项文本颜色 136 .popupColor(0xFFFAF0) // 弹出框文本颜色 137 .selectedBackgroundColor(0xCCCCCC) // 选中项背景颜色 138 .popupBackground(0xD2B48C) // 弹出框背景颜色 139 .usingPopup(true) // 是否显示弹出框 140 .selectedFont({ size: 16, weight: FontWeight.Bolder }) // 选中项字体样式 141 .popupFont({ size: 30, weight: FontWeight.Bolder }) // 弹出框内容的字体样式 142 .itemSize(28) // 每一项的尺寸大小 143 .alignStyle(IndexerAlign.Left) // 弹出框在索引条右侧弹出 144 .popupSelectedColor(0x00FF00) 145 .popupUnselectedColor(0x0000FF) 146 .popupItemFont({ size: 30, style: FontStyle.Normal }) 147 .popupItemBackgroundColor(0xCCCCCC) 148 .onSelect((index: number) => { 149 console.info(this.value[index] + ' Selected!') 150 }) 151 .onRequestPopupData((index: number) => { 152 if (this.value[index] == 'A') { 153 return this.arrayA // 当选中A时,弹出框里面的提示文本列表显示A对应的列表arrayA,选中B、C、L时也同样 154 } else if (this.value[index] == 'B') { 155 return this.arrayB 156 } else if (this.value[index] == 'C') { 157 return this.arrayC 158 } else if (this.value[index] == 'L') { 159 return this.arrayL 160 } else { 161 return [] // 选中其余子母项时,提示文本列表为空 162 } 163 }) 164 .onPopupSelect((index: number) => { 165 console.info('onPopupSelected:' + index) 166 }) 167 } 168 .width('100%') 169 .height('100%') 170 } 171 } 172} 173``` 174 175 176 177### 示例2 178```ts 179// xxx.ets 180@Entry 181@Component 182struct AlphabetIndexerSample { 183 private arrayA: string[] = ['安'] 184 private arrayB: string[] = ['卜', '白', '包', '毕', '丙'] 185 private arrayC: string[] = ['曹', '成', '陈', '催'] 186 private arrayJ: string[] = ['嘉', '贾'] 187 private value: string[] = ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 188 'H', 'I', 'J', 'K', 'L', 'M', 'N', 189 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 190 'V', 'W', 'X', 'Y', 'Z'] 191 @State isNeedAutoCollapse: boolean = false; 192 @State indexerHeight: string = '75%'; 193 194 build() { 195 Stack({ alignContent: Alignment.Start }) { 196 Row() { 197 List({ space: 20, initialIndex: 0 }) { 198 ForEach(this.arrayA, (item: string) => { 199 ListItem() { 200 Text(item) 201 .width('80%') 202 .height('5%') 203 .fontSize(30) 204 .textAlign(TextAlign.Center) 205 } 206 }, (item: string) => item) 207 208 ForEach(this.arrayB, (item: string) => { 209 ListItem() { 210 Text(item) 211 .width('80%') 212 .height('5%') 213 .fontSize(30) 214 .textAlign(TextAlign.Center) 215 } 216 }, (item: string) => item) 217 218 ForEach(this.arrayC, (item: string) => { 219 ListItem() { 220 Text(item) 221 .width('80%') 222 .height('5%') 223 .fontSize(30) 224 .textAlign(TextAlign.Center) 225 } 226 }, (item: string) => item) 227 228 ForEach(this.arrayJ, (item: string) => { 229 ListItem() { 230 Text(item) 231 .width('80%') 232 .height('5%') 233 .fontSize(30) 234 .textAlign(TextAlign.Center) 235 } 236 }, (item: string) => item) 237 } 238 .width('50%') 239 .height('100%') 240 241 Column() { 242 Column() { 243 AlphabetIndexer({ arrayValue: this.value, selected: 0 }) 244 .autoCollapse(this.isNeedAutoCollapse) 245 .height(this.indexerHeight) 246 .selectedColor(0xFFFFFF)// 选中项文本颜色 247 .popupColor(0xFFFAF0)// 弹出框文本颜色 248 .selectedBackgroundColor(0xCCCCCC)// 选中项背景颜色 249 .popupBackground(0xD2B48C)// 弹出框背景颜色 250 .usingPopup(true)// 是否显示弹出框 251 .selectedFont({ size: 16, weight: FontWeight.Bolder })// 选中项字体样式 252 .popupFont({ size: 30, weight: FontWeight.Bolder })// 弹出框内容的字体样式 253 .itemSize(28)// 每一项的尺寸大小 254 .alignStyle(IndexerAlign.Right)// 弹出框在索引条右侧弹出 255 .popupSelectedColor(0x00FF00) 256 .popupUnselectedColor(0x0000FF) 257 .popupItemFont({ size: 30, style: FontStyle.Normal }) 258 .popupItemBackgroundColor(0xCCCCCC) 259 .onSelect((index: number) => { 260 console.info(this.value[index] + ' Selected!'); 261 }) 262 .onRequestPopupData((index: number) => { 263 if (this.value[index] == 'A') { 264 return this.arrayA; 265 } else if (this.value[index] == 'B') { 266 return this.arrayB; 267 } else if (this.value[index] == 'C') { 268 return this.arrayC; 269 } else if (this.value[index] == 'J') { 270 return this.arrayJ; 271 } else { 272 return []; 273 } 274 }) 275 .onPopupSelect((index: number) => { 276 console.info('onPopupSelected:' + index); 277 }) 278 } 279 .height('80%') 280 .justifyContent(FlexAlign.Center) 281 282 Column() { 283 Button('切换成折叠模式') 284 .margin('5vp') 285 .onClick(() => { 286 this.isNeedAutoCollapse = true; 287 }) 288 Button('切换索引条高度到30%') 289 .margin('5vp') 290 .onClick(() => { 291 this.indexerHeight = '30%'; 292 }) 293 Button('切换索引条高度到70%') 294 .margin('5vp') 295 .onClick(() => { 296 this.indexerHeight = '70%'; 297 }) 298 }.height('20%') 299 } 300 .width('50%') 301 .justifyContent(FlexAlign.Center) 302 } 303 .width('100%') 304 .height('100%') 305 } 306 } 307} 308``` 309 310