簡體   English   中英

將初始值設置為android的評級欄中的第1顆星

[英]Set initial value to 1st star in rating bar of android

我的android評級欄有以下代碼:

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:stepSize="1.0" />

我想將第一個星值初始化為-5,這樣剩下的星星就會得到像-4,-3,-2這樣的值......
但是我不知道如何將這個初始值給予android中我的評級欄的第一顆星。
我希望我的評級欄有三種顏色:

  • 對於值-5,-4,-3,-2,-1,星色應為紅色,
  • 對於值0星的顏色應為藍色
  • 對於值1,2,3,4,5,星形顏色應為綠色。

最低評級可以是0,不允許使用負數。

但是,您可以創建一個包含11顆星的評級欄來表示值(-5到+5)

在評級欄的監聽器中,將值映射到-5到+5的范圍(通過從接收的參數中減去6)動態更改顏色,如下所示:

  • 0:藍色
  • 負值:紅色
  • 正值:綠色

所以,輸出看起來像這樣:

在此輸入圖像描述

活動:

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
      private RatingBar ratingBar;
      private TextView tvRating;
      private LayerDrawable stars;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.original_activity_main);

        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        tvRating = (TextView) findViewById(R.id.value);
        stars = (LayerDrawable) ratingBar.getProgressDrawable();

        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float ratingValue,
                boolean fromUser) {

                int value = (int) (ratingValue) - 6;
                tvRating.setText(String.valueOf(value));

                int color = Color.BLUE;

                if(value > 0)
                    color = Color.GREEN;
                else if(value < 0)
                    color = Color.RED;

                    stars.getDrawable(2).setColorFilter(color, PorterDuff.Mode.SRC_ATOP);    
            }
        });
      }

    }

XML:

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

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result : " />

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/label"
        android:text="" />

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/label"
        android:isIndicator="false"
        android:numStars="11"
        android:rating="0.0"
        android:stepSize="1.0" />

</RelativeLayout>

據我所知,您可以設置RatingBar的最小值是0(沒有負數)。

如果將stepSize設置為1.0,則必須使用if條件設置ratingbar。

例如:

if(yourNumber ==(-5)){
ratingBar.setRating(1.0f); //you need to set it using a Float value
} else if (yourNumber ==(-4)){
ratingBar.setRating(2.0f);
} //And so on....

關於更改星形顏色 - 您需要定義自己的評級欄樣式 - 閱讀文章。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM