java - Android 文本框

标签 java android android-edittext

我想将纬度和经度的值返回到我的 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/

相关文章:

java - Assert 中的方法和 junit 5 中的 Assertions 包有什么区别?

java - 在同一事务方法中使用 CriteriaBuilder JPA 插入和更新会出现错误 "Foreign Key not exist"

android - 缩进的 TabLayout

android - Android上可以不带header使用TimePicker吗

android - 显示键盘时隐藏 View

java - Spring AOP 建议不适用于所有 Controller ,仅适用于一个 Controller

java - 如何从该方法中获取方法名称?

android - 如何在android中对日期和空日期进行排序?

android - 我希望我的 EditText 字段在向其输入值时更改其颜色

Android 拦截来 self 自己的应用程序的软击键 - 退格问题