简体   繁体   English

设置Android Ratingbar的等级

[英]Setting the rating of the Android Ratingbar

So I'm trying to set the rating of the RatingBar (RB) widget in code during a touch event. 因此,我试图在触摸事件期间在代码中设置RatingBar(RB)小部件的等级。 Basically, when a user clicks the RB, it has to do some background stuff and then the value of the RB should obviously display the new value. 基本上,当用户单击RB时,它必须做一些背景工作,然后RB的值显然应该显示新值。

Okay let me also state that I'm using the RB as a "favourite" button. 好的,我还要声明,我将RB用作“收藏夹”按钮。 It's probably not the best thing to use but that doesn't mean I can't, right? 可能不是最好用的东西,但这并不意味着我不能,对吧?

The RB consists of only 1 star. RB仅由1星组成。 And if a user clicks it, it adds an item to the favourites list and highlights the star. 如果用户单击它,它将在“收藏夹”列表中添加一个项目并突出显示该星标。 When the user clicks it again, sure enough, the item is removed from the favourites list but the RB does not reset to 0. 当用户再次单击它时,一定会从收藏夹列表中删除该项目,但RB不会重置为0。

favourite_button.setOnTouchListener (new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_UP) {
    if (_favourited) {
      // do stuff
      favourite_button.setRating(0);
      _favourited = false;
    } else {
      // do stuff
      favourite_button.setRating(1);
      _favourited = true;
    }
  }
  return false;
}});

My code works, and it works well, I just can't see why "unfavouriting" doesn't change the RB back to 0. 我的代码可以正常工作,但我看不出为什么“不利”不会将RB更改回0。

So what could be the problem? 那么可能是什么问题呢? When I use "setRating(0)" in a setOnRatingBarChangeListener it sure enough sets the rating to 0. But I can't use the setOnRatingBarChangeListener of the RB because one can't "touch" zero stars. 当我在setOnRatingBarChangeListener中使用“ setRating(0)”时,可以肯定将等级设置为0。但是我不能使用RB的setOnRatingBarChangeListener,因为一个人不能“接触”零星。

how to set it back to 0 when clicked again? 再次单击时如何将其设置回0?

So @Fondesa has pointed out to me as to why you can't set the rating to 0 in the OnTouch event. 因此,@ Fondesa向我指出了为什么您无法在OnTouch事件中将评级设置为0。

The RatingBar is extended from the SeekBar and the listener actually expects a slide action. RatingBar是从SeekBar扩展而来的,并且侦听器实际上期望滑动动作。 Thus the RB can't be set to 0 (or not for this purpose) when there is only one star. 因此,当只有一颗星时,RB不能设为0(或为此目的而不能设为0)。 The proper widget to use for this type of functionality (no surprise here) is the ToggleButton. ToggleButton是用于此类功能的合适的小部件(在此毫不奇怪)。

Rather than using on onTouchListener , you should register an OnRatingBarChangeListener . 您应该注册onTouchListener ,而不是使用OnRatingBarChangeListener This way, you don't have to worry yourself with the implementation of detecting a touch event and whether that should affect the rating bar, all you need worry about is if the rating was changed: 这样,您不必担心检测触摸事件的实现以及它是否会影响评分栏,您只需要担心是否更改了评分即可:

favourite_button.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            _favourited = rating > 0;
        }
    });

http://developer.android.com/reference/android/widget/RatingBar.OnRatingBarChangeListener.html http://developer.android.com/reference/android/widget/RatingBar.OnRatingBarChangeListener.html

That all being said, I assume you're doing more than just setting a boolean. 综上所述,我认为您要做的不仅仅是设置布尔值。 If you're not, you should just get rid of the boolean completely, and whenever you actually need the value, you can just call favourite_button.getRating() 如果不是,则应该完全摆脱布尔值,只要实际需要该值,就可以调用favourite_button.getRating()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM