android中星级评分控件RatingBar的使用

本文详细介绍了Android RatingBar的使用,包括设置属性如isIndicator、numStars、rating、stepSize等,以及如何修改选中星星的颜色。同时展示了不同样式的RatingBar,并给出了XML布局代码和Java监听代码,用于根据评分显示对应的文字描述。
摘要由CSDN通过智能技术生成
一.简单概述

在这里插入图片描述
1.相关属性:

> android:isIndicator:是否用作指示,用户无法更改,默认false
> android:numStars:显示多少个星星,必须为整数 android:rating:默认评分值,必须为浮点数
> android:stepSize: 评分每次增加的值,必须为浮点数
> android:progressBackgroundTint="@color/goCart_btn" :未选中星星的颜色
> android:progressTint="@color/goCart_btn"  :选中星星的颜色
> android:secondaryProgressTint="@color/goCart_btn"  :选中星星的边框颜色


还有两种样式供我们选择

>  style="?android:attr/ratingBarStyleIndicator"
>  style="?android:attr/ratingBarStyleSmall"

2.实现代码

  <TextView
        android:id="@+id/item_tv"
        android:text="星级评分"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RatingBar
        android:rating="3.3"
        android:numStars="5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RatingBar
        style="?android:attr/ratingBarStyleIndicator"
        android:rating="3.3"
        android:numStars="5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RatingBar
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        style="?android:attr/ratingBarStyleSmall"
        android:rating="3.3"
        android:numStars="5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
二.如何修改选中星星的颜色

在这里插入图片描述

 <RatingBar
        android:rating="4.3"
        android:numStars="5"
        android:progressTint="#ffeca011"
        android:secondaryProgressTint="#ffeca011"
        android:progressBackgroundTint="#ffe7e7e7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
三.示例展示

在这里插入图片描述

1.xml文件

这三行是一样的,所以就给出一行代码

 <RelativeLayout
                android:id="@+id/star_describe"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/score_title1"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="描述相符"
                    android:textSize="13sp"
                    android:textColor="@color/theme_defaultText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:id="@+id/describe_tip"
                    android:textSize="12sp"
                    android:textColor="@color/theme_textColor"
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@+id/score_title1"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <RatingBar
                    android:id="@+id/describe_score"
                    android:layout_alignParentRight="true"
                    style="@style/Widget.AppCompat.RatingBar.Indicator"
                    android:isIndicator="false"
                    android:progressTint="@color/goCart_btn"
                    android:progressBackgroundTint="@color/eval_default_star"
                    android:secondaryProgressTint="@color/goCart_btn"
                    android:numStars="5"
                    android:stepSize="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </RelativeLayout>
2.java代码
//监听值的变化
 private void ratingBarListener() {
        describe_score.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                String score = String.valueOf(rating);
                scoreState1 = fun_getScoreState(score);
                describe_tip.setText(scoreState1);
            }
        });
   }
//根据分数转换成文字展示
 private String fun_getScoreState(String score) {
        String rating = "";
        switch (score) {
            case "1.0":
                rating = "差";
                break;
            case "2.0":
                rating = "较差";
                break;
            case "3.0":
                rating = "一般";
                break;
            case "4.0":
                rating = "好";
                break;
            case "5.0":
                rating = "极好";
                break;
        }
        return rating;
    }
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值