Set lower limit of number of stars shown in RatingBar Android

本秂侑毒 提交于 2019-12-10 17:18:02

问题


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);
            }
       }
    });


来源:https://stackoverflow.com/questions/7583637/set-lower-limit-of-number-of-stars-shown-in-ratingbar-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!