Android: How to make onTouch work like onClick?

不羁岁月 提交于 2019-12-12 18:04:14

问题


I used this tutorial http://mobiforge.com/developing/story/using-google-maps-android 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?


回答1:


Check if the move bit is set in the MotionEvent

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



回答2:


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();
    }
}



回答3:


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;
  }


来源:https://stackoverflow.com/questions/4095059/android-how-to-make-ontouch-work-like-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!