首页 > 解决方案 > 在 RatingBar 上设置监听器总是被调用

问题描述

我在我的应用程序中使用 RatingBar 并尝试在用户评分时在其上设置监听器。

xml

<RatingBar
  android:id="@+id/ratingBar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:numStars="5" />

爪哇

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_watch_video);

    (...) //findViewById
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            Toast.makeText(context, "on rating bar changed", Toast.LENGTH_SHORT).show();
        }
    });

}

然而,监听器(setOnRatingBarChangeListener)总是在活动开始时被调用,尽管它上面有任何物理交互。

我的问题可能在这里?

标签: androidratingbar

解决方案


您可以使用 onRatingChanged 函数中的“fromUser”参数进行检查。如果该值为 true,则表示评级已被用户更改。


推荐阅读