16.鸿蒙HarmonyOS App(JAVA)滑块组件Slider与评级组件Rating
ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos: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"
/>
<Slider
ohos:height="match_content"
ohos:width="match_parent"
ohos:min="0"
ohos:max="160"
ohos:step="5"
ohos:progress="20"
/>
<Text
ohos: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"
/>
<Rating
ohos: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 {
@Override
public 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)
{
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}