16.鸿蒙HarmonyOS App(JAVA)滑块组件Slider与评级组件Rating
ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:text_helloworld"ohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="horizontal_center"ohos:text="$string:mainability_HelloWorld"ohos:text_size="30vp"/><Sliderohos:height="match_content"ohos:width="match_parent"ohos:min="0"ohos:max="160"ohos:step="5"ohos:progress="20"/><Textohos:id="$+id:rate2"ohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="horizontal_center"ohos:text="评级组件(Rating)"ohos:text_size="30vp"/><Ratingohos:id="$+id:rating_sce"ohos:height="32vp"ohos:width="150vp"ohos:score="3.5"/></DirectionalLayout>
MainAbilitySlice.java
rating.setGrainSize(1f); //最小的控制粒度 rating.setScore(6f); //当前的评级分数 rating.setIsOperable(false);//设置是否可以交互,true:不可以操作
package com.example.myapplication.slice;import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Rating;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.Resource;public class MainAbilitySlice extends AbilitySlice {@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);Rating rating = (Rating) findComponentById(ResourceTable.Id_rating_sce);rating.setGrainSize(1f); //最小的控制粒度rating.setScore(6f); //当前的评级分数rating.setIsOperable(false);//设置是否可以交互,true:不可以操作Resource resource = null;try{//设置评级分背景:点亮的五角星rating.setFilledElement(new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_star3)));//rating.setHalfFilledElement(new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_star3)));rating.setUnfilledElement(new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_star3a)));}catch (Exception e){}}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
}