Android:更改 key 后如何刷新显示的首选项?

标签 android preferences

我有一个 PreferenceFragment 用于更改几个不同项目的相同设置。显然,不同的项目需要使用不同的 key 存储偏好值。所以我在创建 fragment 后更改了所有首选项的键。

首选项通常由 xml 定义:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="_key_one"
        android:title="Title of first preference" />
    [... and more Preferences]
</PreferenceScreen>

我更改 key 的 PreferenceFragment:

public class ItemSettingPreferenceFragment extends PreferenceFragment {

    private String mItemKey;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mItemKey = getArguments().getString('item_key');     //this will contain name of item
        addPreferencesFromResource(R.xml.item_settings_preferences);
        for (int i=0; i< getPreferenceScreen().getPreferenceCount(); i++){
            final Preference pref = getPreferenceScreen().getPreference(i);
            pref.setKey(mItemKey + pref.getKey());
        }
    }
}

现在所有首选项的每个项目都有一个唯一的键,比如“item1_key_one”
不幸的是,在创建这些首选项后,使用原始键“_key_one”加载并显示值

如何强制首选项重新加载新键的值并显示这些值?

最佳答案

据我所知,“键”就像您偏好的“id”,这意味着您无法在创建偏好“键”后更改它。如果您需要动态生成首选项,那么您可能不应该在 XML 中定义您的首选项。

这是来自 android 示例 API 的示例,它演示了如何从代码而非 XML 生成首选项。

public class PreferencesFromCode extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setPreferenceScreen(createPreferenceHierarchy());
}

private PreferenceScreen createPreferenceHierarchy() {
    // Root
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    // Inline preferences
    PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
    inlinePrefCat.setTitle(R.string.inline_preferences);
    root.addPreference(inlinePrefCat);

    // Checkbox preference
    CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
    checkboxPref.setKey("checkbox_preference");
    checkboxPref.setTitle(R.string.title_checkbox_preference);
    checkboxPref.setSummary(R.string.summary_checkbox_preference);
    inlinePrefCat.addPreference(checkboxPref);

    // Switch preference
    SwitchPreference switchPref = new SwitchPreference(this);
    switchPref.setKey("switch_preference");
    switchPref.setTitle(R.string.title_switch_preference);
    switchPref.setSummary(R.string.summary_switch_preference);
    inlinePrefCat.addPreference(switchPref);

    // Dialog based preferences
    PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
    dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
    root.addPreference(dialogBasedPrefCat);

    // Edit text preference
    EditTextPreference editTextPref = new EditTextPreference(this);
    editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
    editTextPref.setKey("edittext_preference");
    editTextPref.setTitle(R.string.title_edittext_preference);
    editTextPref.setSummary(R.string.summary_edittext_preference);
    dialogBasedPrefCat.addPreference(editTextPref);

    // List preference
    ListPreference listPref = new ListPreference(this);
    listPref.setEntries(R.array.entries_list_preference);
    listPref.setEntryValues(R.array.entryvalues_list_preference);
    listPref.setDialogTitle(R.string.dialog_title_list_preference);
    listPref.setKey("list_preference");
    listPref.setTitle(R.string.title_list_preference);
    listPref.setSummary(R.string.summary_list_preference);
    dialogBasedPrefCat.addPreference(listPref);

    // Launch preferences
    PreferenceCategory launchPrefCat = new PreferenceCategory(this);
    launchPrefCat.setTitle(R.string.launch_preferences);
    root.addPreference(launchPrefCat);

    /*
     * The Preferences screenPref serves as a screen break (similar to page
     * break in word processing). Like for other preference types, we assign
     * a key here so that it is able to save and restore its instance state.
     */
    // Screen preference
    PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this);
    screenPref.setKey("screen_preference");
    screenPref.setTitle(R.string.title_screen_preference);
    screenPref.setSummary(R.string.summary_screen_preference);
    launchPrefCat.addPreference(screenPref);

    /*
     * You can add more preferences to screenPref that will be shown on the
     * next screen.
     */

    // Example of next screen toggle preference
    CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
    nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
    nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference);
    nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference);
    screenPref.addPreference(nextScreenCheckBoxPref);

    // Intent preference
    PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this);
    intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW)
            .setData(Uri.parse("http://www.android.com")));
    intentPref.setTitle(R.string.title_intent_preference);
    intentPref.setSummary(R.string.summary_intent_preference);
    launchPrefCat.addPreference(intentPref);

    // Preference attributes
    PreferenceCategory prefAttrsCat = new PreferenceCategory(this);
    prefAttrsCat.setTitle(R.string.preference_attributes);
    root.addPreference(prefAttrsCat);

    // Visual parent toggle preference
    CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this);
    parentCheckBoxPref.setTitle(R.string.title_parent_preference);
    parentCheckBoxPref.setSummary(R.string.summary_parent_preference);
    prefAttrsCat.addPreference(parentCheckBoxPref);

    // Visual child toggle preference
    // See res/values/attrs.xml for the <declare-styleable> that defines
    // TogglePrefAttrs.
    TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
    CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this);
    childCheckBoxPref.setTitle(R.string.title_child_preference);
    childCheckBoxPref.setSummary(R.string.summary_child_preference);
    childCheckBoxPref.setLayoutResource(
            a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
                    0));
    prefAttrsCat.addPreference(childCheckBoxPref);
    a.recycle();

    return root;
}

在这里您可以在创建首选项之前setKey()

关于Android:更改 key 后如何刷新显示的首选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642915/

相关文章:

java - 当我需要一个 String 时,R.string.XXX 从 strings.xml 返回一个 int

android - Volley Request 将之前的值设置为 TextView

Android - WifiManager disableNetwork() 和 disconnect() 之间有什么区别

android - 为什么我的 QGestureRecognizer 收不到触摸事件?

java - Android:显示传递给第二个 Activity 的数据

Android:禁用键盘锁后无法重新启用

java - 在 PreferenceFragment 中解码时未找到类

android - 阅读/写作偏好是一项昂贵的操作吗?

Delphi:处理用户的字体首选项

ios - UIModalPresentationFormSheet 关闭 iOS 中首选项的事件