如何确定在Android上的长按? [英] How to determine a long touch on android?
问题描述
我正在寻找一种方法,当用户长时间触摸地图视图(可以说1000毫秒)时,我可以执行某些操作.
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).
与Android谷歌地图应用类似,当您长时间触摸时,它会弹出一个气球叠加项.
It would be similar to android google maps app, when you long touch, it brings up a balloon overlay item.
已添加修改
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?
这是我目前正在尝试的操作,但是,即使我只按了电话,它仍表明该事件是action_move,
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,
我在MapActivity中使用内部类
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?
推荐答案
这通常是如何创建onLongClickListener的方法.试试这个:
This is how do you normally create an onLongClickListener. Try this:
mapView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
参考您的修改:
这可能是获得想要的东西的方式.
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);
}
}
}
现在您可以使用checkGlobalVariable函数进行检查:
And now you can check with checkGlobalVariable function:
if(mBooleanIsPressed == true)
这是处理这种情况的方法.祝你好运.
This is how you can handle this case. Good luck.
这篇关于如何确定在Android上的长按?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!