一手遮天 Android - view(进度类): RatingBar 基础

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(进度类): RatingBar 基础

示例如下:

/view/progress/RatingBarDemo1.java

/**
 * RatingBar - 评分条
 *     setOnRatingBarChangeListener(OnRatingBarChangeListener listener) - 评分条发生改变时的回调
 * OnRatingBarChangeListener
 *     onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) - 评分值发生变化
 *         rating - 当前评分值
 *         fromUser - 是否是用户操作导致的评分值变化(比如用户拖动了评分条则此值为 true;程序修改了评分值则此值为 false)
 */

package com.webabcd.androiddemo.view.progress;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

import com.webabcd.androiddemo.R;

public class RatingBarDemo1 extends AppCompatActivity {

    private RatingBar _ratingBar1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_progress_ratingbardemo1);

        _ratingBar1 = (RatingBar)findViewById(R.id.ratingBar1);

        sample();
    }

    private void sample() {
        _ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Toast.makeText(RatingBarDemo1.this, String.format("rating:%f, fromUser:%b", rating, fromUser), Toast.LENGTH_LONG).show();
            }
        });

        // 在 java 中设置评分条的评分值
        _ratingBar1.setRating(4.5f);
    }
}

/layout/activity_view_progress_ratingbardemo1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        RatingBar - 评分条
            numStars - 星星的数量
            stepSize - 评分的最小增量值(0.5 的倍数)
            rating - 评分值(stepSize 的倍数)
            isIndicator - 是否不可修改
    -->

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="3.5"/>

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:numStars="6"
        android:stepSize="1"
        android:rating="3"/>

    <RatingBar
        android:id="@+id/ratingBar3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:isIndicator="true"
        android:rating="4"/>

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:57  webabcd  阅读(44)  评论(0编辑  收藏  举报