gpt4 book ai didi

java - Android 文本框

转载 作者:行者123 更新时间:2023-12-01 15:19:08 28 4
gpt4 key购买 nike

我想将纬度和经度的值返回到我的 EditText 中,我已经能够使用 Toast 来做到这一点,但没有通过 实现它>编辑文本。请帮忙

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

}

最佳答案

只要 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)。您想要像上面的方法或使用 setText(String.valueOf(location.getLatitude())) 那样向 setText() 传递实际的字符串。

希望有帮助。

关于java - Android 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com