我是一名初学者,正在开发一个显示项目的应用程序。用户可以使用评分栏为项目评分,也可以发表评论。等级栏也有刻度。
如何计算该商品的平均评分并将其显示在新的评分栏中?有什么想法吗?
RatingBar mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
EditText mFeedback = (EditText) findViewById(R.id.etFeedback);
Button mSendFeedback = (Button) findViewById(R.id.btnSubmit);
TextView mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
mRatingScale.setText(String.valueOf(v));
switch ((int) ratingBar.getRating()) {
case 1:
mRatingScale.setText("Pretty bad");
break;
case 2:
mRatingScale.setText("Just okay");
break;
case 3:
mRatingScale.setText("Good");
break;
case 4:
mRatingScale.setText("Very good");
break;
case 5:
mRatingScale.setText("Amazing!");
break;
default:
mRatingScale.setText("");
}
}
});
mSendFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mRatingBar.getRating() == 0.0 ) {
Toast.makeText(EventDemo.this, "Please enter a rating", Toast.LENGTH_LONG).show();
} else {
mFeedback.setText("");
mRatingBar.setRating(0);
Toast.makeText(EventDemo.this, "Thanks for the rating", Toast.LENGTH_SHORT).show();
}
}
您可以使用RatingBar.getRating()获取每个等级值,将所有值相加并除以等级栏数。
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingBar2);
float total = 0;
total += ratingBar.getRating();
total += ratingBar1.getRating();
float average = total / 2;
这是您想要的吗?请发表评论以避免误会。谢谢。