2014-12-16 8 views
10

Jak wykonać następujące czynności?jak zmienić kolor RatingBar w zależności od liczby gwiazdek?

jeśli RatingBar ma 1-3 gwiazdki - czerwone gwiazdki. , jeśli RatingBar ma 4 gwiazdki - żółte gwiazdki. jeśli RatingBar ma 5 gwiazdek - zielone gwiazdki.

((RatingBar) layout.findViewById(R.id.ratingBar)).setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate())); 


<RatingBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/ratingBar" android:stepSize="1" android:clickable="false" 
      style="?android:attr/ratingBarStyleSmall" android:layout_gravity="right" 
     android:layout_marginRight="15dp" /> 

EDIT:

@Override 
    public View getItemView(int section, int position, View convertView, ViewGroup parent) { 
     LinearLayout layout; 
     if (convertView == null) { 
      LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      layout = (LinearLayout) inflator.inflate(R.layout.item_survey, null); 
     } else { 
      layout = (LinearLayout) convertView; 
     } 
     ((TextView) layout.findViewById(R.id.questionSurvey)).setText(surveyBeans.get(section).get(position).getQuestion()); 
     RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar)); 
     ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate())); 
     LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); 
     stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 
     stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); 
     ((TextView) layout.findViewById(R.id.commentSurvey)).setText(surveyBeans.get(section).get(position).getComment()); 

     return layout; 
    } 

kod działa po odebraniu @Murtaza Hussain:

RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar)); 
     ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate())); 
     LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); 
     if (ratingBar.getRating()<=3) { 
      stars.getDrawable(2).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); 
     } 
     if(ratingBar.getRating()==4){ 
      stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 
     } 
     if(ratingBar.getRating()==5){ 
      stars.getDrawable(2).setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP); 
     } 
+0

Spójrz na ten post: http://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors – NightFury

Odpowiedz

18
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); 
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); 
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 

LUB

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); 
     stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.starFullySelected), PorterDuff.Mode.SRC_ATOP); 
     stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.starPartiallySelected), PorterDuff.Mode.SRC_ATOP); 
     stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.starNotSelected), PorterDuff.Mode.SRC_ATOP); 

Można to zrobić na

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){ 

     @Override 
     public void onRatingChanged(RatingBar ratingBar, float rating, 
       boolean fromUser) { 
      // This event is fired when rating is changed 
     }  
    }); 

z kodu

@Override 
public View getItemView(int section, int position, View convertView, ViewGroup parent) { 
    LinearLayout layout; 
    if (convertView == null) { 
     LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     layout = (LinearLayout) inflator.inflate(R.layout.item_survey, null); 
    } else { 
     layout = (LinearLayout) convertView; 
    } 
    ((TextView) layout.findViewById(R.id.questionSurvey)).setText(surveyBeans.get(section).get(position).getQuestion()); 

    RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar)); 
    ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate())); 

    if (ratingBar.getRating() == 2f) { 
     //change color of two stars 
    } else if (ratingBar.getRating() == 3f) { 
     //change color of three stars 
    } 

    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); 
    stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 
    stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); 
    ((TextView) layout.findViewById(R.id.commentSurvey)).setText(surveyBeans.get(section).get(position).getComment()); 

    return layout; 
} 
+0

Nie muszę zmieniać ich znaczenia. Jest ustawiony raz na zawsze. Muszę wyświetlać tylko różne gwiazdy. co napisałeś - 3 gwiazdki, jeśli są żółte. drugi szary. jeśli 2 gwiazdki - są żółte - pozostałe są szare. jeśli 5 gwiazdek - wszystkie są żółte. I tak potrzebuję. jeśli 1 lub 2 lub 3 gwiazdki - są czerwone, a reszta - szare. jeśli 4 gwiazdki - 4 gwiazdki to żółty i jeden szary.if 5 gwiazdek - wszystkie 5 - zielone –

+0

nie dostałem. co chcesz ? –

+0

jeśli 1 lub 2 lub 3 gwiazdki - są czerwone, a reszta - szare. jeśli 4 gwiazdki - 4 gwiazdki to żółty i jeden szary. jeśli 5 gwiazdek - wszystkie 5 - zielone –

0
android:progressBackgroundTint="#FFF" 
    android:progressTint="@color/golden" 

zachować te obie linie w RatingBar aktywne gwiazdek kolor będzie złoty. pracuje dla lollipop

Powiązane problemy