2012-06-26 3 views
0

위도와 경도의 값을 EditText으로 되 돌리고 싶습니다. Toast을 사용하여 수행 할 수 있지만 EditText을 통해 처리 할 수는 없습니다. 좋은 말 할 때, 당신은 단순히 당신의 활동은 LocationListener 구현할 수만큼 latEditTextlngEditText 변수가 클래스 넓은 범위를 가지고Android TextBox

 // 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()); 

    } 

답변

1

도움 :

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와 같은 리소스를 참조합니다. 위의 메서드와 같은 실제 String을 setText()에 전달하거나 setText(String.valueOf(location.getLatitude()))을 전달하려고합니다.

희망이 있습니다.

+0

왜 작동하는지 설명하고 싶을 수 있습니다. 즉,'setText'에'int'를 제공하는 것이 그것이 리소스 ID라는 것을 의미하기 때문입니다. – Eric