Set lower limit of number of stars shown in Rating

2019-06-24 03:20发布

问题:

With the RatingBar widget in android you can set the number of stars to be show, but is there a way of setting a lower limit on the number of stars that are always active. As an example rating a film or something where you cannot submit a rating of 1?

回答1:

If this is not possible in the framework strictyl speaking, you could put a default value of 1 with android:rating set to 1, and the add a listener on the RatingBar to go back to 1 when the user tries to go lower no?



回答2:

is there a way of setting a lower limit on the number of stars that are always active.

Yes you can set the Rating using setRating() method. For example:

ratingBar.setRating(rat);


As an example rating a film or something where you cannot submit a rating of 1?

For this, you have to implement a condition inside the onRatingChanged() method. for example:

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

    @Override public void onRatingChanged(RatingBar ratingBar, float rating, 
      boolean fromUser) {
        // implement your condition here
    }
});


回答3:

   <RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    android:rating="1"/>

By setting rating =1 it is shows rating 1.



回答4:

As mentioned by @sephy supporting his answer. I am adding a sample code which I have used in my app.

mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            if(selectedRating<=0) {
                mRatingBar.setRating(1);
            }
       }
    });