如何在一次触摸后停止OnTouch方法

时间:2013-04-03 14:12:07

标签: java android ontouchlistener osmdroid

我有一个小问题。基本上我想做的是绘制路标到地图,用touches.But我想在添加一个路点后停止onTouch方法,因为现在它只需点击一下就可以添加更多路点,并继续添加他们接下来的接触。

继承方法

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK)
    {
        final MapPoint point=(MapPoint) data.getSerializableExtra("result");
         GeoPoint point2 = new GeoPoint(51496994, -134254);
         point.setGeo(point2);



                    view.setOnTouchListener(new View.OnTouchListener()
                    {

                        @Override
                        public boolean onTouch(View v, MotionEvent e) {
                            GeoPoint geo = null;
                            IProjection project = view.getProjection();
                            geo=(GeoPoint)project.fromPixels((int)e.getX(),(int)e.getY());
                            mainDrawer.addGeopointToMap(point.getName(), point.getDesc(), geo);
                            return false;
                        }});
    }

2 个答案:

答案 0 :(得分:2)

您只需将actionListener设置为 null 即可 这将使动作在第一次被调用,然后它将为null,因此不会再次调用。

        view.setOnTouchListener(new View.OnTouchListener()
        {

                    @Override
                    public boolean onTouch(View v, MotionEvent e) {
                        GeoPoint geo = null;
                        IProjection project = view.getProjection();
                        geo=(GeoPoint)project.fromPixels((int)e.getX(),(int)e.getY());
                        mainDrawer.addGeopointToMap(point.getName(), point.getDesc(), geo);
                        view.setOnTouchListener(null)
                        return false;
                    }});
        }

答案 1 :(得分:1)

只需使用布尔标志:

boolean added = false;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK) {
        final MapPoint point=(MapPoint) data.getSerializableExtra("result");
        GeoPoint point2 = new GeoPoint(51496994, -134254);
        point.setGeo(point2);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent e) {
                if(!added) {
                    GeoPoint geo = null;
                    IProjection project = view.getProjection();
                    geo=(GeoPoint)project.fromPixels((int)e.getX(),(int)e.getY());
                    mainDrawer.addGeopointToMap(point.getName(), point.getDesc(), geo);
                    added = true;
                 }
                 return false;
            }});
}