1# rating开发指导 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @liyi0309--> 5<!--Designer: @liyi0309--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9 10rating是评分组件,用于展示用户对某项内容的评价等级。具体用法请参考[rating](../reference/apis-arkui/arkui-js/js-components-basic-rating.md)。 11 12 13## 创建rating组件 14 15在pages/index目录下的hml文件中创建一个rating组件。 16 17 18```html 19<!-- xxx.hml --> 20<div class="container"> 21 <rating></rating> 22</div> 23``` 24 25 26```css 27/* xxx.css */ 28.container { 29 width: 100%; 30 height: 100%; 31 display: flex; 32 justify-content: center; 33 align-items: center; 34 background-color: #F1F3F5; 35} 36.rating { 37 width: 80%; 38 height: 150px; 39} 40``` 41 42 43 44 45## 设置评分星级 46 47rating组件通过设置numstars和rating属性设置评分条的星级总数和当前评星数。 48 49 50```html 51<!-- xxx.hml --> 52<div class="container"> 53 <rating numstars="6" rating="5"> 54 </rating> 55</div> 56``` 57 58 59```css 60/* xxx.css */ 61.container { 62 width: 100%; 63 height: 100%; 64 display: flex; 65 justify-content: center; 66 align-items: center; 67 background-color: #F1F3F5; 68} 69.rating { 70 width: 80%; 71 height: 150px; 72} 73``` 74 75 76 77 78## 设置评分样式 79 80rating组件通过star-background、star-foreground和star-secondary属性设置单个星级未选择、选中和选中的次级背景图片。 81 82 83```html 84<!-- xxx.hml --> 85<div class="container"> 86 <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;"> 87 <rating numstars="5" rating="1" class="myrating" style="width: {{ratewidth}}; height:{{rateheight}}; 88 star-background: {{backstar}}; star-secondary: {{secstar}};star-foreground: {{forestar}};rtl-flip: true;"> 89 </rating> 90 </div> 91</div> 92``` 93 94 95```css 96/* xxx.css */ 97.container { 98 width: 100%; 99 height: 100%; 100 flex-direction: column; 101 align-items: center; 102 justify-content: center; 103 background-color: #F1F3F5; 104} 105``` 106 107 108```js 109// index.js 110export default { 111 data: { 112 backstar: 'common/love.png', 113 secstar: 'common/love.png', 114 forestar: 'common/love1.png', 115 ratewidth: '400px', 116 rateheight: '150px' 117 }, 118 onInit(){ 119 } 120} 121``` 122 123 124 125> **说明:** 126> - star-background、star-secondary、star-foreground属性的星级图源必须全部设置,否则默认的星级颜色为灰色,提示图源设置错误。 127> 128> - star-background、star-secondary、star-foreground属性只支持本地路径图片,图片格式为png和jpg。 129 130 131## 绑定事件 132 133向rating组件添加change事件,打印当前评分。 134 135 136```html 137<!-- xxx.hml --> 138<div class="container"> 139 <rating numstars="5" rating="0" onchange="showrating"></rating> 140</div> 141``` 142 143 144```css 145/* xxx.css */ 146.container { 147 width: 100%; 148 height: 100%; 149 display: flex; 150 justify-content: center; 151 align-items: center; 152 background-color: #F1F3F5; 153} 154.rating { 155 width: 80%; 156 height: 150px; 157} 158``` 159 160 161```js 162// xxx.js 163import promptAction from '@ohos.promptAction'; 164export default { 165 showrating(e) { 166 promptAction.showToast({ 167 message: '当前评分' + e.rating 168 }) 169 } 170} 171``` 172 173 174 175 176## 场景示例 177 178开发者可以通过改变开关状态切换星级背景图,通过改变滑动条的值调整星级总数。 179 180 181```html 182<!-- xxx.hml --> 183<div style="width: 100%;height:100%;flex-direction: column;align-items: center;background-color: #F1F3F5;"> 184 <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;"> 185 <rating numstars="{{stars}}" rating="{{rate}}" stepsize="{{step}}" onchange="showrating" class="myrating" 186 style="width: {{ratewidth}};height:{{rateheight}};star-background: {{backstar}};star-secondary: {{secstar}}; 187 star-foreground: {{forestar}};rtl-flip: true;"></rating> 188 </div> 189 <div style="flex-direction: column;width: 80%;align-items: center;"> 190 <div style="width: 100%;height: 100px;align-items: center;justify-content: space-around;"> 191 <text>替换自定义图片</text> 192 <switch checked="false" showtext="true" onchange="setstar"></switch> 193 </div> 194 <div style="width: 100%;height:120px;margin-top: 50px;margin-bottom: 50px;flex-direction: column;align-items: center; 195 justify-content: space-around;"> 196 <text>numstars {{stars}}</text> 197 <slider id="sli1" min="0" max="10" value="5" step="1" onchange="setnumstars"></slider> 198 </div> 199 <div style="width: 100%;height:120px;flex-direction: column;align-items: center;justify-content: space-around;"> 200 <text>rating {{rate}}</text> 201 <slider id="sli2" min="0" max="10" value="{{rate}}" step="0.5" onchange="setrating"></slider> 202 </div> 203 </div> 204</div> 205``` 206 207 208```css 209/* xxx.css */ 210.myrating:active { 211 width: 500px; 212 height: 100px; 213} 214.switch{ 215 font-size: 40px; 216} 217``` 218 219 220```js 221// xxx.js 222import promptAction from '@ohos.promptAction'; 223export default { 224 data: { 225 backstar: '', 226 secstar: '', 227 forestar: '', 228 stars: 5, 229 ratewidth: '300px', 230 rateheight: '60px', 231 step: 0.5, 232 rate: 0 233 }, 234 onInit(){ 235 }, 236 setstar(e) { 237 if (e.checked == true) { 238 this.backstar = '/common/love.png' 239 this.secstar = 'common/love.png' 240 this.forestar = 'common/love1.png' 241 } else { 242 this.backstar = '' 243 this.secstar = '' 244 this.forestar = '' 245 } 246 }, 247 setnumstars(e) { 248 this.stars = e.progress 249 this.ratewidth = 60 * parseInt(this.stars) + 'px' 250 }, 251 setstep(e) { 252 this.step = e.progress 253 }, 254 setrating(e){ 255 this.rate = e.progress 256 }, 257 showrating(e) { 258 this.rate = e.rating 259 promptAction.showToast({ 260 message: '当前评分' + e.rating 261 }) 262 } 263} 264``` 265 266 267 268## 相关实例 269 270针对rating组件的开发,有以下相关实例可供参考: 271 272- [rating组件的使用(JS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/JSUI/RatingApplication)