我正在开发这个供我自己使用的 Android wearable
应用程序,并且一直致力于如何将变量从一个方法内的 if 语句
传递到另一个方法。 .
@Override
protected void onResume() {
super.onResume();
String myYards = "";
Bundle extras = getIntent().getExtras();
curhole = extras.getInt("hole");
if(curhole == 1){
myYards = "325";
double lat2 = 39.657479;
double lon2 = -121.778788;
}
if(curhole == 2){
myYards = "191";
double lat2 = 39.255478;
double lon2 = -121.125547;
}
TextView holeyard = (TextView) findViewById(R.id.holeYard);
holeyard.setText(String.valueOf(myYards + " yards"));
if(googleApiClient.isConnected()){
requestLocationUpdates();
}
}
@Override
public void onLocationChanged(Location location) {
myLatitude = location.getLatitude();
myLongitude = location.getLongitude();
Location loc1 = new Location("");
loc1.setLatitude(myLatitude);
loc1.setLongitude(myLongitude);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
int myDist = (int) (distanceInMeters * 1.0936);
TextView latView = (TextView) findViewById(R.id.yardage);
latView.setText(String.valueOf(myDist));
}
我想根据 onStart()
方法中的 if 语句
中的整数更改 gps 坐标
并传递它们到 onLocationChanged()
方法,这可能吗?或者我只是犯了这个错误,因为我是 Android 新手..
谢谢!
最佳答案
只需将这些变量放入您的类中,然后类中的所有方法都可以访问它们。
例如在类里面: 双x;
在声明中: x=...
关于java - android 将变量从一种方法传递到另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302372/