gpt4 book ai didi

android - 如何将 getLongitude 或 getLatitude() 的值放入 EditText?

转载 作者:行者123 更新时间:2023-11-29 16:22:52 31 4
gpt4 key购买 nike

如何将 getLongitude() 和 getLatitude() 的值转换为 EditText 可以接受的格式?

如果我像这样把它们放到 Toast 中就可以了

String message = "Current Location \nLongitude: "+location.getLongitude()+"\nLatitude: "+location.getLatitude();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

但如果我像这样将它们放入 EditText,应用程序将停止并出错

tv_obj_long.setText(location.getLongitude());
tv_obj_lat.setText(location.getLatitude());

我认为格式不对,我正在尝试使用 Double.toString() 但它仍然出错

如何解决这个问题?

谢谢

这是我在按钮上的代码

btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showCurrentLocation();
}
});

这是按钮调用的函数

public void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
showToastStatus(location);
tv_obj_long.setText(String.valueOf(location.getLongitude()));
tv_obj_lat.setText(String.valueOf(location.getLatitude()));
}else{
Toast.makeText(getApplicationContext(), "Terjadi Kesalahan dalam pengambilan koordinat", Toast.LENGTH_LONG).show();
}
};

这是有用的 toast

public void showToastStatus(Location location){
String message = "Current Location \nLongitude: "+location.getLongitude()+"\nLatitude: "+location.getLatitude();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

这是我的xml

<TextView
android:text="Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/tv_obj_long"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/tv_obj_lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

帮帮我伙计们 -_-

最佳答案

getLongitudegetLatitude返回双倍。所以在 roder 中将 double 格式化为字符串,你可以使用这样的东西。

String message = String.format("latitude = %f longitude = %f",location.getLatitude(), location.getLongitude());

关于android - 如何将 getLongitude 或 getLatitude() 的值放入 EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450452/

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