其他分享
首页 > 其他分享> > android – RatingBar:相同评级两次

android – RatingBar:相同评级两次

作者:互联网

我的问题是,当我再次评定相同的值时,它没有第二次响应.这意味着当我第一次评价’4’时,我不能再次评价为’4′ ..只有在我评价其他价值而不是’4’时才会回应.

这是我试过的(我需要在我的代码中为getRatingBar执行操作).

public class InteractiveRatingBarActivity extends Activity implements
       OnRatingBarChangeListener {
   RatingBar getRatingBar;
   RatingBar setRatingBar;
   TextView countText;
   int count;
   float curRate;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       findViewsById();

       setRatingBar.setRating(curRate);//overall rating
       getRatingBar.setOnRatingBarChangeListener(this);// ratingbar for user action. 

   }   

   private void findViewsById() {
       getRatingBar = (RatingBar) findViewById(R.id.getRating);
       setRatingBar = (RatingBar) findViewById(R.id.setRating);
       countText = (TextView) findViewById(R.id.countText);
   }


   public void onRatingChanged(RatingBar rateBar, float rating,
           boolean fromUser) {
       DecimalFormat decimalFormat = new DecimalFormat("#.#");
       curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
               / ++count));
       Toast.makeText(InteractiveRatingBarActivity.this,
               "New Rating: " + curRate, Toast.LENGTH_SHORT).show();
       setRatingBar.setRating(curRate);
       countText.setText(count + " Ratings");
   }


}

提前致谢

解决方法:

你可以在评级栏发生变化时调用方法onRatingChanged().当你点击两次相同的速率时,它可能不会改变速率条,所以无法调用onRatingChanged()内的函数另一种可能的方法是在之后调用oncreate
每个onRatingChanged()调用..但它不应该在评级栏中显示以前的速率.

更新:
假设你在onRatingChanged()里面做的任何事情被包装成一个函数,processing().在这里,您最需要的是更改的评级.所以我们将它作为参数传递给函数.尝试以下方式

            float newRating;
    myRatingBar.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        newRating=myRatingBar.getRating();
        processing(newRating);   

        }
    });

标签:android,ratingbar
来源: https://codeday.me/bug/20190825/1719607.html