- xml文件控件
<androidx.appcompat.widget.AppCompatRatingBar
android:id="@+id/star_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating=“3.0”
android:stepSize="1.0"
android:layout_gravity="center">
</androidx.appcompat.widget.AppCompatRatingBar>
相关属性
- numStars:星星的颗数
- stepSize:每次点击会改变的星星个数(float)
- rating:默认的星星数(float)
- java文件事件
/*星级的结果*/
int result=0;
/*找到控件*/
AppCompatRatingBar star_bar = (AppCompatRatingBar) findViewById(R.id.star_bar);
/*设置点击事件*/
star_bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
result=(int)v;//float转换为int型
}
});