android 上如何判断长按?
我正在寻找一种方法,当用户长时间触摸地图视图(比如说 1000 毫秒)时,我可以知道如何执行某个操作。
我将如何判断用户触摸地图视图(或任何视图)的时间长短。
它类似于 Android 谷歌地图应用程序,当您长按时,它会弹出一个气球覆盖项目。
编辑添加
mapView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
上述内容不起作用...有什么想法为什么吗?
编辑添加
这是我目前正在尝试的,但它似乎不起作用,即使我只按手机,它说该事件是一个action_move,
我在我的MapActivity中使用内部类
private long startTime=0;
private long endTime=0;
class MapOverlay extends Overlay {
@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
if(ev.getAction() == MotionEvent.ACTION_DOWN){
//record the start time
startTime = ev.getEventTime();
Log.d("LC", "IN DOWN");
}else if(ev.getAction() == MotionEvent.ACTION_UP){
//record the end time
endTime = ev.getEventTime();
Log.d("LC", "IN UP");
}else if(ev.getAction() == MotionEvent.ACTION_MOVE){
Log.d("LC", "IN move");
endTime=0;
}
//verify
if(endTime - startTime > 1000){
//we have a 1000ms duration touch
//propagate your own event
Log.d("LC", "time touched greater than 1000ms");
Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
startTime=0;
endTime=0;
return true; //notify that you handled this event (do not propagate)
}
return false;//propogate to enable drag
}
}
,这是我的错误日志对我来说没有任何意义,
06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms
结束时间应该设置为零......但它不是......知道为什么吗?
I am looking for a way for when a user long touches a mapview (lets say for 1000ms) that i can some how do a certain action.
How would i go about judging how long a user long touches a mapsview(or any view).
It would be similar to android google maps app, when you long touch, it brings up a balloon overlay item.
Edit added
mapView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
the above does not work... any ideas why?
Edit added
This is what i am trying at the moment, but it does not seem to work, even if i only press on the phone, it says the event is an action_move,
i am using an inner class in my MapActivity
private long startTime=0;
private long endTime=0;
class MapOverlay extends Overlay {
@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
if(ev.getAction() == MotionEvent.ACTION_DOWN){
//record the start time
startTime = ev.getEventTime();
Log.d("LC", "IN DOWN");
}else if(ev.getAction() == MotionEvent.ACTION_UP){
//record the end time
endTime = ev.getEventTime();
Log.d("LC", "IN UP");
}else if(ev.getAction() == MotionEvent.ACTION_MOVE){
Log.d("LC", "IN move");
endTime=0;
}
//verify
if(endTime - startTime > 1000){
//we have a 1000ms duration touch
//propagate your own event
Log.d("LC", "time touched greater than 1000ms");
Toast.makeText(getBaseContext(), "Hello 123", Toast.LENGTH_SHORT).show();
startTime=0;
endTime=0;
return true; //notify that you handled this event (do not propagate)
}
return false;//propogate to enable drag
}
}
and here is my error log that does not make any sense to me
06-29 14:29:55.509: DEBUG/LC(7693): IN move
06-29 14:29:56.149: DEBUG/LC(7693): IN UP
06-29 14:29:56.149: DEBUG/LC(7693): 6346707 6349261
06-29 14:29:56.149: DEBUG/LC(7693): time touched greater than 1000ms
the end time should be set to zero...but it is not...any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是通常创建 onLongClickListener 的方式。试试这个:
参考您的编辑:
这可能是获得您想要的东西的方法。
现在您可以使用 checkGlobalVariable 函数进行检查:
这就是处理这种情况的方法。祝你好运。
This is how do you normally create an onLongClickListener. Try this:
Reference to your edit:
This might be the way to get what you want.
And now you can check with checkGlobalVariable function:
This is how you can handle this case. Good luck.
您可能正在寻找正常的长按?
您必须通过将 android:longClickable 添加到视图 xml 或调用 setLongClickable(true) 将视图设置为可长时间点击。
然后您可以将 OnLongClickListener 添加到视图中。
我不知道有什么方法可以准确确定长点击的时间。但默认的长按与您提到的谷歌地图长按相同。
OnLongClickListener
Your are probably looking for a normal long click?
You will have to set your view to be long clickable by adding android:longClickable to your views xml, or by calling setLongClickable(true).
Then you can add an OnLongClickListener to the view.
I dont know of a way to determine exactly how long the long click is. But the default long click is the same as the google maps long click that you mentioned.
OnLongClickListener
您可以设置一个 longClickListener 和一个 touchListener。添加一个布尔类数据成员变量 longClicked 并将其初始设置为 false。这是设置 longClickListener 的方法。
对于 touchListener
它将给你带来与谷歌地图长按相同的效果。希望有帮助。
You can set up a longClickListener and a touchListener. Add a boolean class data member variable longClicked and set it to false initially. This is how you can set the longClickListener.
For touchListener
It will give you the same effect as google maps long click. Hope it helps.
使用 System.currentTimeMillis() 代替 ev.getEventTime();
use
System.currentTimeMillis()
instead ofev.getEventTime();
当
MotionEvent.ACTION_UP
时,endTime
将被设置为 ev.getEventTime(),这使得当MotionEvent.ACTION_MOVE 时将
不受影响。endTime
设置为零您不应将
endTime
设置为零,而应将startTime
设置为 ev.getEventTime()When
MotionEvent.ACTION_UP
,endTime
will be set to ev.getEventTime(), this make settingendTime
to zero whenMotionEvent.ACTION_MOVE
be not affect.Instead of setting
endTime
to zero, you should setstartTime
to ev.getEventTime()这就是我使用长触摸和短触摸的方式
This is how I use long and short touches