开发者

Android: How to make onTouch work like onClick?

I used this tutorial http://mobiforge.com/developing/story/using-google-maps-androi开发者_开发技巧d to display Map view and set onTouch event to display a toast message (latitude/longitude) when I click on a particular area. Now my problem is that the Toast message is displayed the moment I touch the screen even if it is to move the map? I want to display the toast only if the touch down point and touch up point are the same. How do I implement that?


Check if the move bit is set in the MotionEvent

if(event.getAction() & MotionEvent.ACTION_MOVE == MotionEvent.ACTION_MOVE)


Is this the correct way to do this? I am pretty sure there is a better way.

if (event.getAction() == 0) {                
    touchX = (int) event.getX();
    touchY = (int) event.getY();
}
if(event.getAction() == 1) {
  if(event.getX() == touchX && event.getY()== touchY){
    GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(), (int)event.getY());
       Toast.makeText(getBaseContext(), 
         p.getLatitudeE6() / 1E6 + "," + 
         p.getLongitudeE6() /1E6 , 
         Toast.LENGTH_SHORT).show();
    }
}


This worked for me, when I press down and drag nothing happens, but if I press down and release it worked! just like onClickListener behavior.

  public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_UP){
         // yer Code here ...........
      }

      return false;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜