我的目標是,我希望這個Activity
能夠在用戶導航地圖時移動並顯示敬酒,但只要他點擊中心鍵,它應該返回最後一個值用戶觸摸。onKeyDown問題
由於某些原因,它不工作,當我把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;
}
}
}
你的意思是,當我按下它時,它只是使用它的默認動作,而我寫的代碼是ingored? – Achint 2011-03-14 13:06:34
此外,這聽起來可能不好,但我是新來的java,並感到困惑。我在ontouchevent類之外實現onkeylistener類?在onkeylistener裏面放了onkeydown? – Achint 2011-03-14 13:09:22
這是你需要的那種代碼。 ================================= 公共類<你的活性>延伸活動實現OnKeyListener { @覆蓋 \t公共布爾安其(查看V,INT鍵碼,KeyEvent的事件){ \t \t如果((event.getAction()== KeyEvent.ACTION_DOWN) \t \t \t \t &&(鍵碼== KeyEvent.KEYCODE_DPAD_CENTER)){ \t \t \t \t \t //你的代碼去這裏 \t \t} \t} }請忽略formattinmg :) – amiekuser 2011-03-15 07:34:13