防止单击更改 SeekBar 进度
我在 Android 应用程序中使用 SeekBar
。当用户单击 SeekBar
上的任意位置时,其进度值会发生变化。我只希望当用户滑动 SeekBar
拇指时进度值发生变化(就像 iOS 中的 UISlider
一样)。
我尝试将 SeekBar
的 clickable
属性设置为 false,但这不起作用。我怎样才能实现预期的行为?
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).
I have tried setting the clickable
property of the SeekBar
to false but that didn't work. How can I achieve the desired behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我本周遇到了同样的问题,我使用自定义 SeekBar 解决了它:
按照我的代码:
希望这有帮助
I faced the same issue this week and I resolved it using a custom SeekBar:
following my code:
Hope this helps
查看另一个线程:
用户无法与 Seekbar 交互< /a>
我尝试了以下方法,效果很好。请注意,我的搜索栏将 android:max 属性设置为 100,如下所示:
Check out this other thread:
User can not interact with the Seekbar
I tried the following and it works well. Note my seekbar has android:max property set to 100 like this:
为时已晚,但也许有人需要解决方案。我扩展了自定义 SeekBar 并覆盖了 onTouchEvent。
It's too late, but maybe someone will need the solution. I extend custom SeekBar and override onTouchEvent.
我想出了一个解决方案,它不是很漂亮,但它应该可以解决问题...
想法如下:
创建一个不可见的覆盖层,它将覆盖除拇指按钮之外的所有滑块
(我使用了黑色的搜索栏作为覆盖层)
方法为此,您必须更改覆盖层的大小,以便它覆盖除拇指按钮之外的所有内容(我建议使用两个覆盖层[拇指按钮的每一侧各一个])
当您释放拇指按钮时,您应该然后调整覆盖层的大小
......就像我说的,它并不漂亮。我确信有更好的方法可以做到这一点,但如果你必须完成它,我会尝试这个。
I came up with a solution to this, it's not very pretty, but it should do the trick...
Here's the idea:
Create an invisible overlay that will cover all of the slider except the thumb-button
(I used a blacked out seekbar to act as an overlay)
When the thumb is pressed, call 'bringToFront' method on the slider
Note for this to work you must alter the size of the overlay so that it will cover everything but the thumb-button (I suggest using two overlays [one for each side of the thumb-button])
When you release the thumb-button you should then resize the overlays
... like I said, it ain't pretty. I'm sure there are much better ways to do it, but if you must have it done, I would try this.
这与 SeekBar 内的事件监听器完美配合。
附言。我从 Slim 改进了这段代码,使其更加通用。
This one perfectly work with the event listener inside SeekBar.
PS. I improved this code from Slim to make it more generalize.
您可以在停止跟踪时比较进度。
You can compare progress when stop tracking.
看着@lordmegamax代码,我发现有些东西不起作用。
稍微思考了一下,我找到了解决办法。
希望有帮助。
最好的问候
PS:这是我的第一篇文章,如果我做错了什么,抱歉。
looking at @lordmegamax code, i found somethings that doesnt work.
Thinking a little bit, i found a solution.
Hope it helps.
best regards
PS: this is my first post, sorry if I made something wrong.
通过此代码,您可以禁用用户移动拇指
,其中“时间”是整数变量并保持进度
By this code you can disable thumb to move by user
where "time" is a integer variable and hold the progress
我还没有看到建议的另一个选项:一个子类,它转换接收到的运动事件,以使每个手势看起来都从拇指的中心开始。这对底层搜索栏行为有以下影响:
代码
演示
显示
触摸位置以说明我所描述的行为。
Another option that I don't see suggested yet: a subclass that translates received motion events in order to make every gesture appear to start at the center of the thumb. This has the following impacts on the underlying seek bar behavior:
Code
Demo
Touch location is shown to illustrate the behavior I described.
要禁用 seebBar 上的单击,您可以观察 DOWN 和 UP 事件之间的时间,通常单击的持续时间小于 100 毫秒。
For disable single tap on seebBar you may watch time between DOWN and UP events, Usually duration of single tap less than 100 ms.
我面临的情况是 SeekBar 是由另一段代码创建的,因此我无法使用 onTouchEvent() 处理程序创建子类。相反,我覆盖了我想要处理的实例的 OnTouchListener...
是的,我可以简单地返回“if”表达式的内容,但这不会使代码可读。
The situation I was facing is that the SeekBar was created by another piece of code so I couldn't create a subclass with the onTouchEvent() handler. Instead I overrode the OnTouchListener for the instance I wanted to mess with...
Yes, I could simply return the contents of the "if" expression, but that wouldn't make the code readable.
请参阅此处的答案。它只允许通过拇指而不是可绘制的进度来更改进度。
https://stackoverflow.com/a/62165116/1159930
See answer here. It only allows progress to be changed via the thumb rather than the progress drawable.
https://stackoverflow.com/a/62165116/1159930