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

一.简单概述

在这里插入图片描述
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
    评论
AndroidRatingBar 控件是一种用户评分控件,可以在应用显示一组星形图标,用户可以通过点击星形图标来为应用或产品进行评分RatingBar 控件通常用于应用市场或评估应用的功能RatingBar 控件有以下属性: - android:numStars:指定 RatingBar 星形图标的数量。 - android:rating:指定 RatingBar 的当前评分值。 - android:stepSize:指定评分的步长,例如设置为 0.5,则评分只能是整数或半数(例如 3.0、3.5、4.0 等)。 - android:isIndicator:指定 RatingBar 是否是只读的,即用户是否可以更改评分值。 RatingBar 控件还可以通过监听器来检测评分值的变化。例如,可以使用 OnRatingBarChangeListener 监听器来在评分值发生更改时执行自定义操作。 以下是 RatingBar 控件的示例代码: ``` <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="0.5" android:rating="3.0" android:isIndicator="false" /> ``` 在代码,可以使用 setRating() 方法来动态设置评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setRating(4.5f); ``` 在监听器,可以使用 getRating() 方法来获取当前的评分值,例如: ``` RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Log.d("MyApp", "Rating changed to " + rating); } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值