本文介绍了机器人:图形页面不承认点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是希望有一个图形页面来识别点击,为了调用另一个活动。

到现在为止,我试图正规的onClick,总是在常规视图为我工作,然后用一个onClickListener压倒一切的onTouchEvent以及最后但并非最不重要的。

我所有三种方式为以下code。任何帮助是非常AP preciated。

 进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapView;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;

公共类HelloMapView扩展MapActivity {
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
     最后MapView的地图;
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        地图=(图形页面)findViewById(R.id.mapview);

        map.setOnClickListener(新MapView.OnClickListener(){
         公共无效的onClick(视图v){
          的System.out.println(我听!);
         }
        });
    }
    @覆盖
    保护的布尔isRouteDisplayed(){
        返回false;
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent为arg0){

     的System.out.println(的onTouchEvent事!);
        super.onTouchEvent(为arg0);

        返回super.onTouchEvent(为arg0);
        }

    公共无效的onClick(){
     的System.out.println(的onClick进入!);
    }
}
 

解决方案

添加一个<一个href="http://$c$c.google.com/intl/el-GR/android/add-ons/google-apis/reference/com/google/android/maps/Overlay.html"相对=nofollow>覆盖在地图中查看和处理的onTouchEvent。尝试是这样的:

 公共类MyMapActivity扩展MapActivity {

类MapOverlay扩展com.google.android.maps.Overlay
{
    @覆盖
    公共布尔的onTouchEvent(MotionEvent E,图形页面图形页面)
    {
        如果(e.getAction()== MotionEvent.ACTION_UP){
            的GeoPoint P = MapView.getProjection()在。fromPixels(
                (int)的e.getX(),
                (int)的e.getY());
            MyMapActivity this.startActivityForResult(意向,要求code);
        }
        返回false;
    }
}

      // MyMapActivity方法
      @覆盖
      公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        图形页面=(图形页面)findViewById(R.id.mapview);

        MapOverlay mapOverlay =新MapOverlay();
        名单&LT;覆盖&GT; listOfOverlays =调用MapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
  }
}
 

I simply want a mapview to recognize a click, in order to call another activity.

Until now, i tried the regular "onClick",that always worked for me in regular Views, followed by overriding "onTouchEvent" and last but not least using an onClickListener.

I out all three ways into the following code. Any help is highly appreciated.

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class HelloMapView extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
     final MapView map;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        map = (MapView) findViewById(R.id.mapview);

        map.setOnClickListener(new MapView.OnClickListener() {
         public void onClick(View v){
          System.out.println("I listened!");
         }  
        });
    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {

     System.out.println("onTouchEvent happened!");        
        super.onTouchEvent(arg0);

        return super.onTouchEvent(arg0);
        }

    public void onClick(){
     System.out.println("onClick entered!");
    }    
}
解决方案

Add an Overlay in your map view and handle the OnTouchEvent. Try something like this:

public class MyMapActivity extends MapActivity {

class MapOverlay extends com.google.android.maps.Overlay
{       
    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView) 
    {   
        if (e.getAction() == MotionEvent.ACTION_UP) {                
            GeoPoint p = mapView.getProjection().fromPixels(
                (int) e.getX(),
                (int) e.getY());
            MyMapActivity this.startActivityForResult(intent, requestCode);
        }                            
        return false;
    }        
}

      // MyMapActivity methods
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = (MapView) findViewById(R.id.mapview);

        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
  }
}

这篇关于机器人:图形页面不承认点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 21:50