[英]RatingBar in a AlertDialog : How to change the number of stars programmatically?
[英]how to change the color RatingBar depending on the number of stars?
怎么办?
如果RatingBar具有1-3颗星-红色星。 如果RatingBar有4星-黄色星。 如果RatingBar有5星-绿色星。
((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" />
编辑:
@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;
}
回答@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);
}
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
要么
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);
你可以这样做
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// This event is fired when rating is changed
}
});
从你的代码
@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;
}
android:progressBackgroundTint="#FFF"
android:progressTint="@color/golden"
将这两行都保留在RatingBar中,活跃的星星颜色将变为金色。 为棒棒糖工作
这是我的解决方案,当等级小于或等于3时,星形颜色将变为红色,大于3且小于5的星形将变为黄色,最后等于5则变为绿色。
好吧,我发现您需要通过代码更改xml中的progressTint,因此我发现progressTintList在ColorStateList的帮助下可以解决问题。
ratingBar.setOnRatingBarChangeListener(object: RatingBar.OnRatingBarChangeListener{
override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {
ratingBar?.numStars = 5
if (rating <= 3){
ratingBar?.progressTintList = ColorStateList.valueOf(Color.RED)
}else if (rating > 3 && rating < 5){
ratingBar?.progressTintList = ColorStateList.valueOf(Color.YELLOW)
}else{
ratingBar?.progressTintList = ColorStateList.valueOf(Color.GREEN)
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.