onKeyDown的问题

时间:2011-03-14 12:56:26

标签: android

我的目标是,我希望这个Activity能够在用户导航地图时移动并显示祝酒词,但是当他点击中心键时,它应该返回最后一个值用户触摸了。

由于某些原因它无法工作,当我将onKeyDown置于if循环中时,它会产生错误。我如何让它工作?

    public class createNote extends MapActivity {

    MapView mapview;
    String sLatt;
    String sLonn;

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }       

    @Override
    public void onCreate(Bundle savedInstanceStates){       
        super.onCreate(savedInstanceStates);
        setContentView(R.layout.map);

        mapview=(MapView)findViewById(R.id.MapView);
        mapview.setBuiltInZoomControls(true);

        mapOverlay mapoverlay=new mapOverlay();
        List<Overlay>listOfOverlays=mapview.getOverlays();
        listOfOverlays.add(mapoverlay);


    }   


    class mapOverlay extends com.google.android.maps.Overlay{
        @Override

        public boolean onTouchEvent(MotionEvent event, MapView mapview){
            GeoPoint p=null;

            if (event.getAction()==1){
                 p=mapview.getProjection().fromPixels((int)event.getX(), (int)event.getY());
                Toast.makeText(getBaseContext(),p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6, Toast.LENGTH_SHORT).show();



            double lat=(double) (p.getLatitudeE6()/1E6);
            double lon=(double) (p.getLongitudeE6()/1E6);

            String sLat=Double.toString(lat);
            String sLon=Double.toString(lon);
            sLatt=sLat;
            sLonn=sLon;

            Toast.makeText(getBaseContext(),sLat + "-" + sLon,Toast.LENGTH_SHORT).show();


            //setResult(RESULT_OK, new Intent().setAction(sLat + "," + sLon));
            //setResult(RESULT_OK, new Intent().setAction("test"));
            //Intent data = new Intent();
            //data.putExtra("key1", sLat);
            //data.putExtra("key2", sLon);
            //setResult(RESULT_OK,data);
            //finish();



            }



            return false;
        }

        public boolean onKeyDown(int keyCode, KeyEvent event){
             super.onKeyDown(keyCode, event, mapview);
             if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
                    Intent data = new Intent();
                    data.putExtra("key1", sLatt);
                    data.putExtra("key2", sLonn);
                    setResult(RESULT_OK,data);
                    finish();
                    return true;
             }
             else return false;

        }
    }


}

2 个答案:

答案 0 :(得分:1)

您的活动需要实现OnKeyListener,然后您可以覆盖onKeyDown()。

答案 1 :(得分:0)

您将onKeyDown放在mapOverlay班级而不是Activity