Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

從零開始學 Android(二十三):用 RatingBar 做評分功能

編輯:關於 Android 程式設計

要讓使用者替應用打分,「點星星」大概是最直覺的互動方式。Android 提供了 RatingBar 元件來實作這個需求,它繼承自 SeekBar,因此本質上就是一個只能離散取值的進度條。

類別繼承結構:

java.lang.Object
  └ android.view.View
      └ android.widget.ProgressBar
          └ android.widget.AbsSeekBar
              └ android.widget.RatingBar

常用方法

  • RatingBar(Context context):建構子。
  • getNumStars():取得評分總數(星星有幾顆)。
  • getRating():取得目前評分。
  • getStepSize():取得每次變動的步長。
  • isIndicator() / setIsIndicator(boolean):是否為純顯示模式(不可操作)。
  • setMax(int):設定最大值。
  • setNumStars(int):設定星星數量,對應 XML 屬性 android:numStars。
  • setRating(float):設定目前評分,對應 android:rating。
  • setStepSize(float):設定步長,對應 android:stepSize。
  • setOnRatingBarChangeListener(...):設定評分變動監聽。

版面配置

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:numStars="5"
        android:rating="3"
        android:stepSize="0.5"
        android:isIndicator="false" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ratingBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

isIndicator="false" 表示使用者可以操作;若只是要展示歷史評分,把它設為 true 會更合適。

Java 端:把分數換成有意義的文案

public class MainActivity extends Activity {
    private RatingBar ratingBar;
    private TextView textView;

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

        ratingBar = findViewById(R.id.ratingBar1);
        textView = findViewById(R.id.textView1);

        ratingBar.setOnRatingBarChangeListener(
                (bar, rating, fromUser) -> {
                    switch ((int) rating) {
                        case 1: textView.setText("當前的用戶評價:太糟了"); break;
                        case 2: textView.setText("當前的用戶評價:不太好"); break;
                        case 3: textView.setText("當前的用戶評價:不錯哦"); break;
                        case 4: textView.setText("當前的用戶評價:很好哦"); break;
                        case 5: textView.setText("當前的用戶評價:太棒了"); break;
                        default: break;
                    }
                });
    }
}

依分數顯示不同文案的效果:

實務上要注意的地方

  • fromUser 參數很有用。當你透過程式呼叫 setRating() 時,fromUser 會是 false;只有使用者親手點擊才是 true。若要在程式初始化時設定分數又不觸發後續邏輯,記得判斷這個旗標,否則容易出現無限迴圈或重複上報。
  • 步長與型別。stepSize="0.5" 會讓 RatingBar 出現半顆星的樣式,但顯示出來的分數是 float;若要存進資料庫或做統計,建議先決定好精度規則。
  • 自訂星星樣式。透過 android:progressDrawable 指定一組 selector(包含背景、次要進度、主要進度三層),就能換成愛心、金幣等自訂圖示。
  • 記得保存狀態。螢幕旋轉後 RatingBar 的值可能重設,需在 onSaveInstanceState() 中保存 getRating() 並於重建後還原。

評分元件讓使用者能低成本地回饋意見,是很值得放進設定頁或引導流程的小功能。

下節預告:事件處理的幾種設定方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved