繁体   English   中英

android将变量从一种方法传递到另一种方法

[英]android pass variable from one method to another

我正在为自己使用的这个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));
}

我想基于onStart()方法中if statements中的整数更改gps coordinates ,并将它们传递给onLocationChanged()方法,这可能吗? 还是我刚遇到这个错误,因为我是Android新手。

谢谢!

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

例如在课堂上:double x;

在状态下:x = ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM