1. 程式人生 > Android入門教學 >Android 評分條 RatingBar

Android 評分條 RatingBar

本節將繼續學習一個和進度有關的控制元件:RatingBar ,在 Android 中 RatingBar 是一個可以支援使用者打分的 UI 控制元件,相比 ProgressBar 而言,RatingBar 不僅僅可以用來展示同時還可以接收使用者的輸入操作;而相比 SeekBar,RatingBar 則更側重於與使用者的互動性。有了前兩節的基礎,這一節理解起來也是非常容易的。

1. RatingBar 的特性

從繼承關係來看,RatingBar 是派生自 SeekBar 的,所以它擁有 SeekBar 的所有屬性和功能(當然也包括 ProgressBar 的功能)。可以理解為 SeekBar 是進度的另一種表現形式,它將進度換成了分數,使用者拖動進度條來進行評分操作,我們先來感受一下 RatingBar 的樣式:

RatingBar樣例

有圖有真相,我們會在各大 App 市場、電影票 App、團購 App 等場景中大量的看到 RatingBar 的影子。

使用者通過點選不同的星級進行打分,通過 RatingBar 我們可以拿到一個浮點型別的數字,比如:1.0、2.3、5.5 等等,就類似於我們給電影評分,接下來我們看看如何使用。

2 RatingBar 的基本用法

2.1 RatingBar 的屬性

RatingBar 的屬性都很好理解,以下是幾個常用的屬性:

  • android:rating:
    設定預設評分值,接收一個浮點數。
  • android:numStars:
    總共有多少顆星級,接收一個整形數。
  • android:isIndicator:

    設定當前 RatingBar 是否只是分數展示,即使用者無法更改分值,接收一個 boolean 值。
  • android:stepSize:
    評分增加的步進值,接收一個浮點數。
  • style:
    自定義樣式。和 ProgressBar、SeekBar 一樣,我們可以自定義 RatingBar 的展示樣式,系統為我們提供了兩種常用的樣式可供選擇:
    style="?android:attr/ratingBarStyleSmall"
    style="?android:attr/ratingBarStyleIndicator"
    

2.2 RatingBar 事件監聽及 API

RatingBar 需要接收使用者的輸入,所以需要一個事件監聽器:

RatingBar.OnRatingBarChangeListener

介面中只定義了一個函式:

void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser);

引數說明:

  • ratingBar: 分值被修改的 RatingBar 例項
  • rating: 修改之後的分值,一個浮點型
  • fromUser: 是否是由使用者主動修改

另外我們在程式碼中可以動態去修改 Rating 值以及總的星級數,通過如下介面:

setRating(float rating)          // 動態設定當前分值
setNumStars(final int numStars)  // 動態設定總分數

通過以下介面獲取當前的分值及總分數:

getNumStars()
getRating()

2.3 RatingBar 的使用示例

接下來我們實現一個完整的 RatingBar 工程,通過打分回撥介面及獲取分數的 API 完成以下兩大功能:

  • 在點選 RatingBar 進行打分之後通過 Toast 展示當前分數
  • 點選 Button 的獲取當前分數,並展示到 TextView 之上
    首先我們編寫佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="200dp"
        android:numStars="5"
        android:rating="2.6"
        android:stepSize="0.1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ratingBar"
        android:layout_alignLeft="@+id/ratingBar"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="30dp"
        android:text="獲取評分" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_marginTop="20dp"
        android:textSize="20dp"
        android:textStyle="bold" />
</RelativeLayout>

佈局檔案中我們放置了 3 個控制元件:RatingBar 用來展示星級、Button 用來展示觸發分數獲取、TextView用來展示具體的分數值,接下來編寫 Java 程式碼完成邏輯控制:

package com.emercy.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private RatingBar mRatingBar;
    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRatingBar = findViewById(R.id.ratingBar);
        mTextView = findViewById(R.id.textView);
        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int numStars = mRatingBar.getNumStars();
                float rating = mRatingBar.getRating();
                mTextView.setText("得分: " + rating + "/" + numStars);
            }
        });

        mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Toast.makeText(MainActivity.this, "當前分數變動為:" + rating, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

首先給 Button 設定一個點選事件監聽器,在其中通過getNumStars()getRating()獲取總的星數和當前星數,並通過 TextView 展示。接著給 RatingBar 。

設定一個評分變動監聽器,在評分修改的時候通過 Toast 打印出最新的評分,效果如下:

圖片描述

3. 小結

本節是繼續前兩節—— ProgressBar 、 SeekBar 的又一升級版,它延續了進度條及拖動條的特點,而將場景轉移到了評分系統當中,使用起來和 ProgressBar、SeekBar 類似,也非常好理解,在具體場景的時候能夠靈活運用就行。