简体   繁体   English

单星评级栏

[英]Single star RatingBar

I'm trying to display a RatingBar View that is only capable of showing a one star rating, or no stars. 我正在尝试显示一个只能显示一星评级或没有星级的RatingBar视图。 This seems so trivial... 这似乎是微不足道的......

My ratingbar is defined like so: 我的评级栏定义如下:

My view implements OnRatingBarChangeListener and OnTouchListener 我的视图实现了OnRatingBarChangeListener和OnTouchListener

My OnRatingBarChange handler has no code. 我的OnRatingBarChange处理程序没有代码。 My OnTouch handler looks like so: 我的OnTouch处理程序如下所示:

@Override
public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        if(this.mRating.getRating() == 0)
        {
            this.mRating.setRating(1);
        }
        else
        {
            this.mRating.setRating(0);
        }
        return true;
    }
    return false;
}

No matter what, the onTouch event does not succeed at setting the rating back to zero. 无论如何,onTouch事件都无法将评级设置回零。 This seems way too trivial. 这似乎太微不足道了。 What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

Consider using ToggleButton and btn_star stock drawable instead. 考虑使用ToggleButtonbtn_star stock drawable。 Here's example: 这是一个例子:

    <ToggleButton
        android:button="@android:drawable/btn_star"
        android:background="@android:color/transparent"
        android:textOn=""
        android:textOff=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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(或不用于此目的)。 The proper widget to use for this type of functionality (no surprise here) is the ToggleButton. 用于此类功能的适当小部件(这里不足为奇)是ToggleButton。

I had the same problem, trying to use a single star RatingBar as a favourite button. 我遇到了同样的问题,试图使用一个明星RatingBar作为最喜欢的按钮。 But the ToggleButton is exactly what you need for this type of thing. 但是ToggleButton正是你需要的这类东西。

This is what I implemented for the ToggleButton. 这是我为ToggleButton实现的。 (The _favourited bool was specifically used for something in my code, I can't remember now) _favourited bool专门用于代码中的某些内容,我现在不记得了)

favourite_button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    if (!_favourited) {
      // Do stuff                  
      _favourited = true;
    } else {
      // Do stuff                 
      _favourited = false;
    }
}});

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

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