android将变量从一个方法传递给另一个方法

时间:2016-09-03 01:06:18

标签: java android variables if-statement methods

我正在使用这个Android wearable应用程序供我自己使用,并坚持如何将方法中的if statement内的变量传递给另一个方法。

@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));
}

我想根据gps coordinates方法中if statements内的整数更改onStart()并将其传递给onLocationChanged()方法,这可能吗?或者我只是因为我是新手来解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

只需将这些变量放在您的类中,然后类中的所有方法都可以访问它们。

例如在课堂上: 双x;

in statematent: x = ...