0

我想将纬度和经度的值返回到我EditText的 s 中,我已经能够使用 a 来做到这一点,Toast但没有通过EditText. 请帮助

       // EditText latEditText = (EditText)findViewById(R.id.lat);
//EditText lngEditText = (EditText)findViewById(R.id.lng);

protected void showCurrentLocation(){
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null){
        String message = String.format(
                                   "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                                   location.getLongitude(), location.getLatitude()
                           );
                           Toast.makeText(LayActivity.this, message,
                                   Toast.LENGTH_LONG).show();
        //latEditText.setText(nf.format(location.getLatitude()).toString());
        //lngEditText.setText(nf.format(location.getLongitude()).toString());
    }
}

private class MyLocationListener implements LocationListener{

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        String message = String.format(
                  "New Location \n Longitude: %1$s \n Latitude:  %2$s",
                  location.getLongitude(), location.getLatitude()
                  );
               Toast.makeText(LayActivity.this, message, Toast.LENGTH_LONG).show();


        //latEditText.setText((int) location.getLatitude());
        //lngEditText.setText((int) location.getLongitude());

    }
4

1 回答 1

1

只要latEditTextlngEditText变量具有类范围,您就可以简单地让您的 Activity 实现 LocationListener:

public class Example extends Activity implements LocationListener

然后这个工作:

public void onCreate(Bundle savedInstanceState) {
    ...
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(location != null)
        displayLocation(location);
}

public void onLocationChanged(Location location) {
    if(location != null) 
        displayLocation(location);
}

public void displayLocation(Location location) {
        latEditText.setText(location.getLatitude() + "");
        lngEditText.setText(location.getLongitude() + "");
    }
}

按照要求

Soxxeh 指出您注释掉的代码正在传递 setText() 一个整数,这样做引用了一个资源(例如您的 strings.xml 中字符串的唯一 ID)。您想像上面的方法或 with 一样传递 setText() 实际的 String setText(String.valueOf(location.getLatitude()))

希望有帮助。

于 2012-06-26T19:35:11.913 回答