我有一个带有设置项的小应用程序。在这个类中,我已将onPreferenceClick设置为从GPS获取坐标。当监听器返回时,我的希望是自动设置经纬度文本编辑框。我尝试了所有可用的源代码示例,但没有成功:
public void onLocationChanged(Location l) {
Log.d("H","Location Received: "+l.toString());
prefLocation.setSummary(l.toString());
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor1 = settings.edit();
editor1.putString("posLat","xxx");
editor1.commit();
}
当我点击PreferenceScreen时,执行此代码并且位置侦听器返回时,“posLat”键的EditTextPreference仍显示旧值。我正在为弄清楚问题而疯狂!我的prefs.xml:<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="My_Shared_Preferences">
<PreferenceCategory
android:title="Your Location">
<PreferenceScreen
android:title="Find Location..."
android:key="location"
android:summary="Click here to read your location automatically"/>
<EditTextPreference
android:title="Latitude"
android:key="posLat" />
<EditTextPreference
android:title="Longtitude"
android:key="posLong" />
<EditTextPreference
android:title="Altitude"
android:key="posAlt" />
</PreferenceCategory>
</PreferenceScreen>
或者,也许有更好的方法来存储应用程序的位置值?我不想让用户手动输入坐标,但是我也不想使用保存和加载文本文件的设置,这似乎太粗糙了。