problem with onKeyDown
My aim here is that I want this Activity
to be able to move around and show the toasts while the user navigates the map, but as soon as he hits the center key, it should return the last value that the user touched.
Its not working for some reason, and when I put onKeyDown
inside the if l开发者_Python百科oop, it gives errors. How do I get it to work?
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;
}
}
}
Your activity needs to implement the OnKeyListener and then you can override the onKeyDown().
You are placing onKeyDown
inside the mapOverlay
class instead of the Activity
.
精彩评论