gpt4 book ai didi

android - 关于安卓首选项

转载 作者:行者123 更新时间:2023-11-29 21:15:41 26 4
gpt4 key购买 nike

我的问题是,当我试图从 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;
}
}

非常感谢任何帮助。我也可以成为新手的好榜样 =)

最佳答案

编辑器、提交和首选项的所有这些都不起作用 - 改变它!

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>

关于android - 关于安卓首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21503049/

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