android - 在 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)总是在活动开始时被调用,尽管它上面有任何物理交互。
我的问题可能在这里?
解决方案
您可以使用 onRatingChanged 函数中的“fromUser”参数进行检查。如果该值为 true,则表示评级已被用户更改。
推荐阅读
- c# - 实体框架代码优先:生产环境 - Id 不能为空
- windows - 如何在不杀死其他 cmds 的情况下杀死一个 cmd 进程?
- c# - 当我在数据库中进行更改时,触发器是否会触发 RowChanged 事件?
- vba - 参数不是可选的 - 简单的 VBA 函数
- python - 对于 scipy.stats.f_oneway 中的 F 值,“inf”是什么意思?
- java - Spring JPA 使用单引号内的参数编写本机查询
- r - 如何预测许多产品的时间序列,包括 R 中的季节性因素
- java - Java - 解析未格式化的字符串
- android - Context.startForegroundService 然后没有调用 Service.startForeground
- angular - 角材料在 slackblitz 中不起作用