Java 如何确定android上的长时间触摸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6519748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine a long touch on android?
提问by molleman
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.
我正在寻找一种方法,当用户长时间触摸地图视图(可以说 1000 毫秒)时,我可以了解如何执行某个操作。
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,
这就是我目前正在尝试的,但它似乎不起作用,即使我只按下电话,它也说该事件是 action_move,
i am using an inner class in my MapActivity
我在 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?
结束时间应该设置为零......但它不是......知道为什么吗?
回答by snowstreams
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.
您可能正在寻找正常的长按?您必须通过将 android:longClickable 添加到您的视图 xml 或通过调用 setLongClickable(true) 将您的视图设置为可长时间点击。然后您可以将 OnLongClickListener 添加到视图中。我不知道有什么方法可以确定长按的确切时间。但是默认的长按与您提到的谷歌地图长按相同。
回答by user1884608
use System.currentTimeMillis()
instead of ev.getEventTime();
使用System.currentTimeMillis()
代替ev.getEventTime();
回答by eleven
When MotionEvent.ACTION_UP
, endTime
will be set to ev.getEventTime(), this make setting endTime
to zero when MotionEvent.ACTION_MOVE
be not affect.
Instead of setting endTime
to zero, you should set startTime
to ev.getEventTime()
时MotionEvent.ACTION_UP
,endTime
将设置为 ev.getEventTime(),这endTime
在MotionEvent.ACTION_MOVE
不影响时设置为零。endTime
您应该设置startTime
为 ev.getEventTime()而不是设置为零
回答by Umit Kaya
This is how do you normally create an onLongClickListener. Try this:
这就是您通常如何创建 onLongClickListener。尝试这个:
mapView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
Reference to your edit:
参考您的编辑:
This might be the way to get what you want.
这可能是获得您想要的东西的方式。
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
checkGlobalVariable();
}
};
// Other init stuff etc...
@Override
public void onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Execute your Runnable after 1000 milliseconds = 1 second.
handler.postDelayed(runnable, 1000);
mBooleanIsPressed = true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mBooleanIsPressed) {
mBooleanIsPressed = false;
handler.removeCallbacks(runnable);
}
}
}
And now you can check with checkGlobalVariable function:
现在您可以使用 checkGlobalVariable 函数进行检查:
if(mBooleanIsPressed == true)
This is how you can handle this case. Good luck.
这就是您可以处理这种情况的方式。祝你好运。
回答by Jeetendra Gan
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.
您可以设置一个 longClickListener 和一个 touchListener。添加一个布尔类数据成员变量 longClicked 并最初将其设置为 false。这是设置 longClickListener 的方法。
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
longClicked = true;
return false;
}
});
For touchListener
对于触摸监听器
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(longClicked){
//Do whatever you want here!!
longClicked = false;
}
return false;
}
});
It will give you the same effect as google maps long click. Hope it helps.
它会给你和谷歌地图长按一样的效果。希望能帮助到你。