LongClick event happens too quickly. How can I increase the clicktime required to trigger it?(LongClick 事件发生得太快.如何增加触发它所需的点击时间?)
问题描述
在我正在处理的应用程序中,我要求用户必须单击 &在某个动作发生之前持有一个组件一段时间.
In an application I'm working on, I have the requirement that a user must click & hold a component for a period time before a certain action occurs.
我目前正在使用OnLongClickListener来监听longclick,但是我发现触发OnLongClick事件的点击时长太短了.
I'm currently using an OnLongClickListener to listen for the longclick, but I find that the length of a click to trigger the OnLongClick event is too short.
例如,假设 LongClick 事件在单击 400 毫秒后触发,但我希望用户必须单击 &在事件触发前保持 1200ms.
For example, let's say the LongClick event triggers after a 400ms click, but I want the user to have to click & hold for 1200ms before the event triggers.
有什么方法可以将 LongClick 事件配置为需要更长的点击时间?
或者是否有其他构造可以让我听到更长的点击?
Is there any way I can configure the LongClick event to require a longer click?
Or is there perhaps another construct that would allow me to listen for longer clicks?
推荐答案
onLongClick事件不能改变定时器,由android自己管理.
It is not possible to change the timer on the onLongClick event, it is managed by android itself.
可以使用 .setOnTouchListener().
What is possible is to use .setOnTouchListener().
然后在 MotionEvent 为 ACTION_DOWN 时注册.
请注意变量中的当前时间.
然后当一个带有 ACTION_UP 的 MotionEvent 被注册并且 current_time - actionDown 时间 > 1200 毫秒时,然后做一些事情.
Then register when the MotionEvent is a ACTION_DOWN.
Note the current time in a variable.
Then when a MotionEvent with ACTION_UP is registered and the current_time - actionDown time > 1200 ms then do something.
差不多:
Button button = new Button();
long then = 0;
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
then = (Long) System.currentTimeMillis();
}
else if(event.getAction() == MotionEvent.ACTION_UP){
if(((Long) System.currentTimeMillis() - then) > 1200){
return true;
}
}
return false;
}
})
这篇关于LongClick 事件发生得太快.如何增加触发它所需的点击时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LongClick 事件发生得太快.如何增加触发它所需的点击时间?
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
