防止单击更改SeekBar进度 [英] Prevent single tap from changing SeekBar progress
本文介绍了防止单击更改SeekBar进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Android应用中使用的是 SeekBar
。当用户在 SeekBar
上的任意位置单击时,其进度值将更改。我只希望在用户滑动 SeekBar
拇指时更改进度值(就像iOS中的 UISlider
一样)。
I am using a SeekBar
in my Android app. When a user single taps anywhere on the SeekBar
, its progress value is changed. I only want the progress value to change when the user slides the SeekBar
thumb (just like a UISlider
in iOS).
我尝试设置 SeekBar
clickable 属性>为假,但这没有用。如何实现所需的行为?
I have tried setting the clickable
property of the SeekBar
to false but that didn't work. How can I achieve the desired behavior?
推荐答案
我本周遇到了相同的问题,并使用自定义的SeekBar解决了该问题:
遵循我的代码:
I faced the same issue this week and I resolved it using a custom SeekBar: following my code:
public class Slider extends SeekBar {
private Drawable mThumb;
public Slider(Context context) {
super(context);
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
mThumb = thumb;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getX() >= mThumb.getBounds().left
&& event.getX() <= mThumb.getBounds().right
&& event.getY() <= mThumb.getBounds().bottom
&& event.getY() >= mThumb.getBounds().top) {
super.onTouchEvent(event);
} else {
return false;
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
return false;
} else {
super.onTouchEvent(event);
}
return true;
}}
希望这会有所帮助
这篇关于防止单击更改SeekBar进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文