【发布时间】:2010-05-28 17:54:35
【问题描述】:
好的,这就是我所拥有的。
Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),MapClass.class);
startActivityForResult(myIntent,0);
}
});
这个方法打开了我的 MapClass 类,目前我刚刚设置为显示一个地方的位置。
但是我有很多按钮,而不是为每个按钮制作很多不同的 mapClass 类,我想知道我可以只使用一个类,并且根据按下的按钮“id”,它会检查一个“ if语句',然后将正确的坐标放入方法中以显示地图。这比编写 20-30 个类要简洁得多。
我不确定我的解释是否正确,如果不告诉我。
感谢您的帮助。
这是我现在的地图类...
public class MapClass extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
int button = getIntent().getExtras().getInt("button", -1);
switch(button){
case DundrumSelector.BUTTON1:
handleCoordinates("53.288719","-6.241179");
break;
case DundrumSelector.BUTTON2:
handleCoordinates("53.288719","-6.241179");
break;
}
}
private void handleCoordinates(String l, String b){
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = {l, b};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat*1E6),
(int) (lng*1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
然后我的其他活动是
public static final int BUTTON1 = R.id.anandaAddressButton;
Button anandabutton = (Button) findViewById(R.id.anandaAddressButton);
anandabutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),MapClass.class);
myIntent.putExtra("button", BUTTON1);
startActivityForResult(myIntent,0);
}
});
由于某种原因,当我尝试单击列表中除 BUTTON1 之外的任何其他项目时,程序崩溃了。我不得不取出 BUTTON2、BUTTON3 等,因为如果我尝试进入他们的视野,它只会不断崩溃。但是当我注释掉这个 onClick 方法时,它们都可以正常工作吗?
你们有任何意见吗?你明白我的问题吗? 谢谢。
【问题讨论】:
标签: android google-maps button onclick