mol*_*man 16 java android touch android-mapview
我正在寻找一种方式,当用户长时间触摸mapview(比方说1000ms),我可以做一些特定的动作.
我将如何判断用户长时间触摸mapsview(或任何视图)的时间.
这将类似于Android谷歌地图应用程序,当你长时间触摸,它会带来气球覆盖项目.
编辑已添加
mapView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
以上不起作用......任何想法为什么?
这就是我现在正在尝试的,但它似乎不起作用,即使我只按手机,它说这个事件是一个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
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误日志,对我没有任何意义
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
Run Code Online (Sandbox Code Playgroud)
结束时间应该设置为零...但它不是......任何想法为什么?
这是你通常如何创建onLongClickListener.试试这个:
mapView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
参考您的编辑:
这可能是获得你想要的东西的方法.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以使用checkGlobalVariable函数进行检查:
if(mBooleanIsPressed == true)
Run Code Online (Sandbox Code Playgroud)
这是你如何处理这种情况.祝好运.
归档时间: |
|
查看次数: |
18937 次 |
最近记录: |