我的麻烦在于,当我尝试从listpreference中选择一个项目时,摘要会从数组settings_listofitems_keys
更改为所选的数组键。
但是我希望在布局上将摘要设置为数组settings_listofitems_values
中的用户可读格式,同时将settings_listofitems_keys
中的值保存为首选项中的参数。
Xml数组文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_listofitems_values">
<item>Green</item>
<item>Orange</item>
</string-array>
<string-array name="settings_listofitems_keys">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/settings_select_horo" >
<ListPreference
android:defaultValue="@string/settings_selectitem"
android:key="preferenceitem"
android:entries="@array/settings_listofitems_values"
android:summary="@string/settings_plsselect"
android:entryValues="@array/settings_listofitems_keys"
android:title="@string/settings_plsselect" />
</PreferenceCategory>
</PreferenceScreen>
代码:
public class UserSettingActivity extends PreferenceActivity implements
OnPreferenceChangeListener {
private static final String APP_PREFERENCES = "mypref";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
SharedPreferences settings = getSharedPreferences(APP_PREFERENCES,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
ListPreference myid = (ListPreference)
findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
editor.commit();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString());
return true;
}
}
非常感谢任何帮助。我也可以成为新手的一个很好的例子=)
答案 0 :(得分:1)
而不是
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener {
private static final String APP_PREFERENCES = "mypref";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
SharedPreferences settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
ListPreference myid = (ListPreference) findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
editor.commit();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString());
return true;
}
尝试使用:
public class UserSettingActivity extends PreferenceActivity {
private static final String APP_PREFERENCES = "mypref";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
ListPreference myid = (ListPreference) findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int index = ((ListPreference) preference).findIndexOfValue(newValue.toString());
preference.setSummary(index >= 0 ? ((ListPreference) preference).getEntries()[index] : null);
return true;
}
});
}
}
希望这会有所帮助:)
答案 1 :(得分:1)
所有这一切都与编辑和提交和首选项没有任何关系 - 改变它!
public class UserSettingActivity extends PreferenceActivity implements
OnPreferenceChangeListener {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_preferences);
ListPreference myid = (ListPreference)
findPreference("preferenceitem");
myid.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString()); // that's ok
return true;
}
}
这是您的问题 - 您必须删除摘要行:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/settings_select_horo" >
<ListPreference
android:defaultValue="@string/settings_selectitem"
android:key="preferenceitem"
android:entries="@array/settings_listofitems_values"
<!-- android:summary="@string/settings_plsselect"-->
android:entryValues="@array/settings_listofitems_keys"
android:title="@string/settings_plsselect" />
</PreferenceCategory>
</PreferenceScreen>