1e41f4b71Sopenharmony_ci# Rating 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci提供在给定范围内选择评分的组件。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 子组件 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci无 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 接口 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciRating(options?: RatingOptions) 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**参数:** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 28e41f4b71Sopenharmony_ci| ------ | ----------------------------------------- | ---- | -------------- | 29e41f4b71Sopenharmony_ci| rating | [RatingOptions](#ratingoptions13对象说明) | 否 | 设置评分组件。 | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## 属性 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci### stars 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_cistars(value: number) 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci设置评分总数。设置为小于等于0的值时,按默认值显示。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**参数:** 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 48e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------------- | 49e41f4b71Sopenharmony_ci| value | number | 是 | 设置评分总数。<br/>默认值:5 | 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci### stepSize 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_cistepSize(value: number) 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci设置操作评级的步长。设置为小于0.1的值时,按默认值显示。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**参数:** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 66e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ----------------------------------------------------------- | 67e41f4b71Sopenharmony_ci| value | number | 是 | 操作评级的步长。<br/>默认值:0.5<br/>取值范围:[0.1, stars] | 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci### starStyle 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_cistarStyle(options: StarStyleOptions) 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci设置评分的样式。该属性所支持的图片类型能力参考[Image](ts-basic-components-image.md)组件。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci支持加载本地图片和网络图片,暂不支持PixelMap类型和Resource资源。 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci默认图片加载方式为异步,暂不支持同步加载。 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**参数:** 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 88e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | 89e41f4b71Sopenharmony_ci| options | [StarStyleOptions](#starstyleoptions13对象说明) | 是 | 评分的样式。<br/>**说明:** <br/>backgroundUri或者foregroundUri或者secondaryUri设置的图片路径错误时,图片不显示。<br/>backgroundUri或者foregroundUri设置为undefined或者空字符串时,rating会选择加载系统默认星型图源。<br/>secondaryUri不设置或者设置的值为undefined或者空字符串时,优先设置为backgroundUri,效果上等同于只设置了foregroundUri、backgroundUri。 | 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci> **说明:** 92e41f4b71Sopenharmony_ci> 93e41f4b71Sopenharmony_ci> rating宽高为[width, height]时,单个图片的绘制区域为[width / stars, height]。 94e41f4b71Sopenharmony_ci> 95e41f4b71Sopenharmony_ci> 为了指定绘制区域为方形,建议自定义宽高时采取[height * stars, height], width = height * stars的方式。 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci### contentModifier<sup>12+</sup> 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_cicontentModifier(modifier: ContentModifier\<RatingConfiguration>) 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci定制Rating内容区的方法。 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**参数:** 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 110e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 111e41f4b71Sopenharmony_ci| modifier | [ContentModifier\<RatingConfiguration>](#ratingconfiguration12对象说明) | 是 | 在Rating组件上,定制内容区的方法。<br/>modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。 | 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci## 事件 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci### onChange 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_cionChange(callback:(value: number) => void) 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci操作评分条的评星发生改变时触发该回调。 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**参数:** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 131e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------- | 132e41f4b71Sopenharmony_ci| value | number | 是 | 评分条的评分。 | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci## 键盘走焦规格 135e41f4b71Sopenharmony_ci| 按键 | 功能描述 | 136e41f4b71Sopenharmony_ci|------------|-----------------------------| 137e41f4b71Sopenharmony_ci| Tab | 组件间切换焦点。 | 138e41f4b71Sopenharmony_ci| 左右方向键 | 评分预览增加/减少(步长为step),不改变实际分值。 | 139e41f4b71Sopenharmony_ci| Home | 移动到第一个星星, 不改变实际分值。 | 140e41f4b71Sopenharmony_ci| End | 移动到最后一个星星, 不改变实际分值。 | 141e41f4b71Sopenharmony_ci| Space/Enter | 根据当前评分提交评分结果。 | 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci## RatingConfiguration<sup>12+</sup>对象说明 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci开发者需要自定义class实现ContentModifier接口。 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 152e41f4b71Sopenharmony_ci| ------ | ------ | ------ |-------------------------------- |-------------------------------- | 153e41f4b71Sopenharmony_ci| rating | number | 否 | 否 |评分条当前评分数。<br/>默认值:0 | 154e41f4b71Sopenharmony_ci| indicator | boolean | 否 | 否 | 评分条是否作为一个指示器。<br/>默认值:false | 155e41f4b71Sopenharmony_ci| stars | number | 否 | 否 |评分条的星级总数。<br/>默认值:5 | 156e41f4b71Sopenharmony_ci| stepSize | number | 否 | 否 |评分条的评分步长。<br/>默认值:0.5 | 157e41f4b71Sopenharmony_ci| triggerChange | Callback\<number> | 否 | 否 |触发评分数量变化。 | 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci## RatingOptions<sup>13+</sup>对象说明 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**卡片能力:** 从API version 13开始,该接口支持在ArkTS卡片中使用。 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 168e41f4b71Sopenharmony_ci| --------- | ------- | ---- | ------------------------------------------------------------ | 169e41f4b71Sopenharmony_ci| rating | number | 是 | 设置并接收评分值。<br/>默认值:0<br/>取值范围: [0, stars]<br/>小于0取0,大于stars取最大值stars。<br />该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 170e41f4b71Sopenharmony_ci| indicator | boolean | 否 | 设置评分组件作为指示器使用,不可改变评分。<br/>默认值:false, 可进行评分<br/>**说明:** <br/>indicator=true时,默认组件高度height=12.0vp,组件width=height * stars。 <br/>indicator=false时,默认组件高度height=28.0vp,组件width=height * stars。 | 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci## StarStyleOptions<sup>13+</sup>对象说明 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**卡片能力:** 从API version 13开始,该接口支持在ArkTS卡片中使用。 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 181e41f4b71Sopenharmony_ci| ------------- | ------ | ---- | ------------------------------------------------------------ | 182e41f4b71Sopenharmony_ci| backgroundUri | string | 是 | 未选中的星级的图片链接,可由用户自定义或使用系统默认图片。 | 183e41f4b71Sopenharmony_ci| foregroundUri | string | 是 | 选中的星级的图片路径,可由用户自定义或使用系统默认图片。 | 184e41f4b71Sopenharmony_ci| secondaryUri | string | 否 | 部分选中的星级的图片路径,可由用户自定义或使用系统默认图片。 | 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci## 示例 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci### 示例1 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci```ts 191e41f4b71Sopenharmony_ci// xxx.ets 192e41f4b71Sopenharmony_ci@Entry 193e41f4b71Sopenharmony_ci@Component 194e41f4b71Sopenharmony_cistruct RatingExample { 195e41f4b71Sopenharmony_ci @State rating: number = 3.5 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci build() { 198e41f4b71Sopenharmony_ci Column() { 199e41f4b71Sopenharmony_ci Column() { 200e41f4b71Sopenharmony_ci Rating({ rating: this.rating, indicator: false }) 201e41f4b71Sopenharmony_ci .stars(5) 202e41f4b71Sopenharmony_ci .stepSize(0.5) 203e41f4b71Sopenharmony_ci .margin({ top: 24 }) 204e41f4b71Sopenharmony_ci .onChange((value: number) => { 205e41f4b71Sopenharmony_ci this.rating = value 206e41f4b71Sopenharmony_ci }) 207e41f4b71Sopenharmony_ci Text('current score is ' + this.rating) 208e41f4b71Sopenharmony_ci .fontSize(16) 209e41f4b71Sopenharmony_ci .fontColor('rgba(24,36,49,0.60)') 210e41f4b71Sopenharmony_ci .margin({ top: 16 }) 211e41f4b71Sopenharmony_ci }.width(360).height(113).backgroundColor('#FFFFFF').margin({ top: 68 }) 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci Row() { 214e41f4b71Sopenharmony_ci Image('common/testImage.jpg') 215e41f4b71Sopenharmony_ci .width(40) 216e41f4b71Sopenharmony_ci .height(40) 217e41f4b71Sopenharmony_ci .borderRadius(20) 218e41f4b71Sopenharmony_ci .margin({ left: 24 }) 219e41f4b71Sopenharmony_ci Column() { 220e41f4b71Sopenharmony_ci Text('Yue') 221e41f4b71Sopenharmony_ci .fontSize(16) 222e41f4b71Sopenharmony_ci .fontColor('#182431') 223e41f4b71Sopenharmony_ci .fontWeight(500) 224e41f4b71Sopenharmony_ci Row() { 225e41f4b71Sopenharmony_ci Rating({ rating: 3.5, indicator: false }).margin({ top: 1, right: 8 }) 226e41f4b71Sopenharmony_ci Text('2021/06/02') 227e41f4b71Sopenharmony_ci .fontSize(10) 228e41f4b71Sopenharmony_ci .fontColor('#182431') 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci }.margin({ left: 12 }).alignItems(HorizontalAlign.Start) 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci Text('1st Floor') 233e41f4b71Sopenharmony_ci .fontSize(10) 234e41f4b71Sopenharmony_ci .fontColor('#182431') 235e41f4b71Sopenharmony_ci .position({ x: 295, y: 8 }) 236e41f4b71Sopenharmony_ci }.width(360).height(56).backgroundColor('#FFFFFF').margin({ top: 64 }) 237e41f4b71Sopenharmony_ci }.width('100%').height('100%').backgroundColor('#F1F3F5') 238e41f4b71Sopenharmony_ci } 239e41f4b71Sopenharmony_ci} 240e41f4b71Sopenharmony_ci``` 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci### 示例2 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci```ts 247e41f4b71Sopenharmony_ci// xxx.ets 248e41f4b71Sopenharmony_ci@Entry 249e41f4b71Sopenharmony_ci@Component 250e41f4b71Sopenharmony_cistruct RatingExample { 251e41f4b71Sopenharmony_ci @State rating: number = 3.5 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci build() { 254e41f4b71Sopenharmony_ci Column() { 255e41f4b71Sopenharmony_ci Rating({ rating: this.rating, indicator: false }) 256e41f4b71Sopenharmony_ci .stars(5) 257e41f4b71Sopenharmony_ci .stepSize(0.5) 258e41f4b71Sopenharmony_ci .starStyle({ 259e41f4b71Sopenharmony_ci backgroundUri: '/common/imag1.png', // common目录与pages同级 260e41f4b71Sopenharmony_ci foregroundUri: '/common/imag2.png', 261e41f4b71Sopenharmony_ci secondaryUri: '/common/imag3.png' 262e41f4b71Sopenharmony_ci }) 263e41f4b71Sopenharmony_ci .margin({ top: 24 }) 264e41f4b71Sopenharmony_ci .onChange((value: number) => { 265e41f4b71Sopenharmony_ci this.rating = value 266e41f4b71Sopenharmony_ci }) 267e41f4b71Sopenharmony_ci Text('current score is ' + this.rating) 268e41f4b71Sopenharmony_ci .fontSize(16) 269e41f4b71Sopenharmony_ci .fontColor('rgba(24,36,49,0.60)') 270e41f4b71Sopenharmony_ci .margin({ top: 16 }) 271e41f4b71Sopenharmony_ci }.width('100%').height('100%').backgroundColor('#F1F3F5') 272e41f4b71Sopenharmony_ci } 273e41f4b71Sopenharmony_ci} 274e41f4b71Sopenharmony_ci``` 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci### 示例3 279e41f4b71Sopenharmony_ci该示例实现了自定义评分条的功能,每个圆圈表示0.5分。ratingIndicator为true时表示评分条作为一个指示器不可改变评分; 280e41f4b71Sopenharmony_ci为false时可以进行评分。ratingStars可改变评分总数。ratingStepsize可改变评分步长。 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci```ts 283e41f4b71Sopenharmony_ci// xxx.ets 284e41f4b71Sopenharmony_ciclass MyRatingStyle implements ContentModifier<RatingConfiguration> { 285e41f4b71Sopenharmony_ci name: string = "" 286e41f4b71Sopenharmony_ci style: number = 0 287e41f4b71Sopenharmony_ci constructor(value1: string, value2: number) { 288e41f4b71Sopenharmony_ci this.name = value1 289e41f4b71Sopenharmony_ci this.style = value2 290e41f4b71Sopenharmony_ci } 291e41f4b71Sopenharmony_ci applyContent() : WrappedBuilder<[RatingConfiguration]> { 292e41f4b71Sopenharmony_ci return wrapBuilder(buildRating) 293e41f4b71Sopenharmony_ci } 294e41f4b71Sopenharmony_ci} 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci@Builder function buildRating(config: RatingConfiguration) { 297e41f4b71Sopenharmony_ci Column() { 298e41f4b71Sopenharmony_ci Row() { 299e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 300e41f4b71Sopenharmony_ci .fill(config.rating >= 0.4 ? Color.Black : Color.Red) 301e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 302e41f4b71Sopenharmony_ci if (!config.indicator) { 303e41f4b71Sopenharmony_ci if (config.stepSize = 0.5) { 304e41f4b71Sopenharmony_ci config.triggerChange(0.5); 305e41f4b71Sopenharmony_ci return 306e41f4b71Sopenharmony_ci } 307e41f4b71Sopenharmony_ci if (config.stepSize = 1) { 308e41f4b71Sopenharmony_ci config.triggerChange(1); 309e41f4b71Sopenharmony_ci return 310e41f4b71Sopenharmony_ci } 311e41f4b71Sopenharmony_ci } 312e41f4b71Sopenharmony_ci }).visibility(config.stars >= 1 ? Visibility.Visible : Visibility.Hidden) 313e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 314e41f4b71Sopenharmony_ci .fill(config.rating >= 0.9 ? Color.Black : Color.Red) 315e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 316e41f4b71Sopenharmony_ci if (!config.indicator) { 317e41f4b71Sopenharmony_ci config.triggerChange(1); 318e41f4b71Sopenharmony_ci } 319e41f4b71Sopenharmony_ci }).visibility(config.stars >= 1 ? Visibility.Visible : Visibility.Hidden) 320e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 321e41f4b71Sopenharmony_ci .fill(config.rating >= 1.4 ? Color.Black : Color.Red) 322e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 323e41f4b71Sopenharmony_ci if (!config.indicator) { 324e41f4b71Sopenharmony_ci if (config.stepSize = 0.5) { 325e41f4b71Sopenharmony_ci config.triggerChange(1.5); 326e41f4b71Sopenharmony_ci return 327e41f4b71Sopenharmony_ci } 328e41f4b71Sopenharmony_ci if (config.stepSize = 1) { 329e41f4b71Sopenharmony_ci config.triggerChange(2); 330e41f4b71Sopenharmony_ci return 331e41f4b71Sopenharmony_ci } 332e41f4b71Sopenharmony_ci } 333e41f4b71Sopenharmony_ci }).visibility(config.stars >= 2 ? Visibility.Visible : Visibility.Hidden).margin({left:10}) 334e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 335e41f4b71Sopenharmony_ci .fill(config.rating >= 1.9 ? Color.Black : Color.Red) 336e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 337e41f4b71Sopenharmony_ci if (!config.indicator) { 338e41f4b71Sopenharmony_ci config.triggerChange(2); 339e41f4b71Sopenharmony_ci } 340e41f4b71Sopenharmony_ci }).visibility(config.stars >= 2 ? Visibility.Visible : Visibility.Hidden) 341e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 342e41f4b71Sopenharmony_ci .fill(config.rating >= 2.4 ? Color.Black : Color.Red) 343e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 344e41f4b71Sopenharmony_ci if (!config.indicator) { 345e41f4b71Sopenharmony_ci if (config.stepSize = 0.5) { 346e41f4b71Sopenharmony_ci config.triggerChange(2.5); 347e41f4b71Sopenharmony_ci return 348e41f4b71Sopenharmony_ci } 349e41f4b71Sopenharmony_ci if (config.stepSize = 1) { 350e41f4b71Sopenharmony_ci config.triggerChange(3); 351e41f4b71Sopenharmony_ci return 352e41f4b71Sopenharmony_ci } 353e41f4b71Sopenharmony_ci } 354e41f4b71Sopenharmony_ci }).visibility(config.stars >= 3 ? Visibility.Visible : Visibility.Hidden).margin({left:10}) 355e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 356e41f4b71Sopenharmony_ci .fill(config.rating >= 2.9 ? Color.Black : Color.Red) 357e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 358e41f4b71Sopenharmony_ci if (!config.indicator) { 359e41f4b71Sopenharmony_ci config.triggerChange(3); 360e41f4b71Sopenharmony_ci } 361e41f4b71Sopenharmony_ci }).visibility(config.stars >= 3 ? Visibility.Visible : Visibility.Hidden) 362e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 363e41f4b71Sopenharmony_ci .fill(config.rating >= 3.4 ? Color.Black : Color.Red) 364e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 365e41f4b71Sopenharmony_ci if (!config.indicator) { 366e41f4b71Sopenharmony_ci if (config.stepSize = 0.5) { 367e41f4b71Sopenharmony_ci config.triggerChange(3.5); 368e41f4b71Sopenharmony_ci return 369e41f4b71Sopenharmony_ci } 370e41f4b71Sopenharmony_ci if (config.stepSize = 1) { 371e41f4b71Sopenharmony_ci config.triggerChange(4); 372e41f4b71Sopenharmony_ci return 373e41f4b71Sopenharmony_ci } 374e41f4b71Sopenharmony_ci } 375e41f4b71Sopenharmony_ci }).visibility(config.stars >= 4 ? Visibility.Visible : Visibility.Hidden).margin({left:10}) 376e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 377e41f4b71Sopenharmony_ci .fill(config.rating >= 3.9 ? Color.Black : Color.Red) 378e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 379e41f4b71Sopenharmony_ci if (!config.indicator) { 380e41f4b71Sopenharmony_ci config.triggerChange(4); 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci }).visibility(config.stars >= 4 ? Visibility.Visible : Visibility.Hidden) 383e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 384e41f4b71Sopenharmony_ci .fill(config.rating >= 4.4 ? Color.Black : Color.Red) 385e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 386e41f4b71Sopenharmony_ci if (!config.indicator) { 387e41f4b71Sopenharmony_ci if (config.stepSize = 0.5) { 388e41f4b71Sopenharmony_ci config.triggerChange(4.5); 389e41f4b71Sopenharmony_ci return 390e41f4b71Sopenharmony_ci } 391e41f4b71Sopenharmony_ci if (config.stepSize = 1) { 392e41f4b71Sopenharmony_ci config.triggerChange(5); 393e41f4b71Sopenharmony_ci return 394e41f4b71Sopenharmony_ci } 395e41f4b71Sopenharmony_ci } 396e41f4b71Sopenharmony_ci }).visibility(config.stars >= 5 ? Visibility.Visible : Visibility.Hidden).margin({left:10}) 397e41f4b71Sopenharmony_ci Circle({ width: 25, height: 25 }) 398e41f4b71Sopenharmony_ci .fill(config.rating >= 4.9 ? Color.Black : Color.Red) 399e41f4b71Sopenharmony_ci .onClick((event: ClickEvent) => { 400e41f4b71Sopenharmony_ci if (!config.indicator) { 401e41f4b71Sopenharmony_ci config.triggerChange(5); 402e41f4b71Sopenharmony_ci } 403e41f4b71Sopenharmony_ci }).visibility(config.stars >= 5 ? Visibility.Visible : Visibility.Hidden) 404e41f4b71Sopenharmony_ci } 405e41f4b71Sopenharmony_ci Text("分值:" + config.rating) 406e41f4b71Sopenharmony_ci } 407e41f4b71Sopenharmony_ci} 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci@Entry 410e41f4b71Sopenharmony_ci@Component 411e41f4b71Sopenharmony_cistruct ratingExample { 412e41f4b71Sopenharmony_ci @State rating: number = 0; 413e41f4b71Sopenharmony_ci @State ratingIndicator: boolean = true; 414e41f4b71Sopenharmony_ci @State ratingStars: number = 0; 415e41f4b71Sopenharmony_ci @State ratingStepsize: number = 0.5; 416e41f4b71Sopenharmony_ci @State ratingEnabled: boolean = true; 417e41f4b71Sopenharmony_ci build() { 418e41f4b71Sopenharmony_ci Row() { 419e41f4b71Sopenharmony_ci Column() { 420e41f4b71Sopenharmony_ci Rating({ 421e41f4b71Sopenharmony_ci rating: 0, 422e41f4b71Sopenharmony_ci indicator: this.ratingIndicator 423e41f4b71Sopenharmony_ci }) 424e41f4b71Sopenharmony_ci .stepSize(this.ratingStepsize) 425e41f4b71Sopenharmony_ci .stars(this.ratingStars) 426e41f4b71Sopenharmony_ci .backgroundColor(Color.Transparent) 427e41f4b71Sopenharmony_ci .width('100%') 428e41f4b71Sopenharmony_ci .height(50) 429e41f4b71Sopenharmony_ci .onChange((value: number) => { 430e41f4b71Sopenharmony_ci console.info('Rating change is'+ value); 431e41f4b71Sopenharmony_ci this.rating = value 432e41f4b71Sopenharmony_ci }) 433e41f4b71Sopenharmony_ci .contentModifier(new MyRatingStyle("hello", 3)) 434e41f4b71Sopenharmony_ci Button(this.ratingIndicator ? "ratingIndicator : true" : "ratingIndicator : false") 435e41f4b71Sopenharmony_ci .onClick((event) => { 436e41f4b71Sopenharmony_ci if (this.ratingIndicator) { 437e41f4b71Sopenharmony_ci this.ratingIndicator = false 438e41f4b71Sopenharmony_ci } else { 439e41f4b71Sopenharmony_ci this.ratingIndicator = true 440e41f4b71Sopenharmony_ci } 441e41f4b71Sopenharmony_ci }).margin({top : 5}) 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci Button(this.ratingStars < 5 ? "ratingStars + 1, ratingStars =" + this.ratingStars : "ratingStars最大值为5") 444e41f4b71Sopenharmony_ci .onClick((event) => { 445e41f4b71Sopenharmony_ci if (this.ratingStars < 5) { 446e41f4b71Sopenharmony_ci this.ratingStars += 1 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci }).margin({top : 5}) 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci Button(this.ratingStars > 0 ? "ratingStars - 1, ratingStars =" + this.ratingStars : "ratingStars小于等于0时默认等于5") 451e41f4b71Sopenharmony_ci .onClick((event) => { 452e41f4b71Sopenharmony_ci if (this.ratingStars > 0) { 453e41f4b71Sopenharmony_ci this.ratingStars -= 1 454e41f4b71Sopenharmony_ci } 455e41f4b71Sopenharmony_ci }).margin({top : 5}) 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci Button(this.ratingStepsize == 0.5 ? "ratingStepsize : 0.5" : "ratingStepsize : 1") 458e41f4b71Sopenharmony_ci .onClick((event) => { 459e41f4b71Sopenharmony_ci if (this.ratingStepsize == 0.5) { 460e41f4b71Sopenharmony_ci this.ratingStepsize = 1 461e41f4b71Sopenharmony_ci } else { 462e41f4b71Sopenharmony_ci this.ratingStepsize = 0.5 463e41f4b71Sopenharmony_ci } 464e41f4b71Sopenharmony_ci }).margin({top : 5}) 465e41f4b71Sopenharmony_ci } 466e41f4b71Sopenharmony_ci .width('100%') 467e41f4b71Sopenharmony_ci .height('100%') 468e41f4b71Sopenharmony_ci .justifyContent(FlexAlign.Center) 469e41f4b71Sopenharmony_ci } 470e41f4b71Sopenharmony_ci .height('100%') 471e41f4b71Sopenharmony_ci } 472e41f4b71Sopenharmony_ci} 473e41f4b71Sopenharmony_ci``` 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci