是否可以将Android“偏好设置/设置"屏幕配置为使用“默认"共享偏好设置以外的其他偏好设置? [英] Can Android Preferences/Settings screen be configured to use preferences other than 'DEFAULT' shared preferences?

查看:199
本文介绍了是否可以将Android“偏好设置/设置"屏幕配置为使用“默认"共享偏好设置以外的其他偏好设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android教程和示例显示了如何使用首选项片段和首选项活动在应用中创建设置屏幕.

Android Tutorials and examples show how to use Preference Fragment and Preference Activity to create Settings screen in an app.

但是,以这种方式公开的所有首选项都将其值设置为" DefaultSharedPreferences ".

However, all preferences exposed this way get their values to 'DefaultSharedPreferences'.

如果我们要将首选项值保存到其他(非默认/自定义)SharedPreferences,该怎么办.

What if we want to save preference values to other (non-default / custom) SharedPreferences.

我想到的用例类似于电子邮件应用程序,在该应用程序中,应用程序可以允许用户创建多个帐户,还可以为用户提供不同的方式来保存不同帐户的设置.我可以选择的一种方法是在首选项"屏幕中以编程方式"添加首选项,然后为每个首选项指定一个不同的共享首选项"目标(取决于帐户).

The use case I have in mind is similar to email apps where an app may allow the user to create multiple accounts, and also give the user options to save settings for different accounts differently. One method that I see as an option would have been to 'programatially' add preference in the Preference screens, and specify a different Shared Preference target for each one of them (depending on account).

但是,似乎当前所有添加到PreferenceFragment/Activity的首选项都将其值保存到DefaultSharedPreferences.

However, it seems that currently all preferences added to PreferenceFragment/Activity get their values saved to DefaultSharedPreferences.

任何对此的指点将不胜感激.

Any pointers on this will be appreciated.

一个

推荐答案

从技术上讲,这三行代码将从一侧完成工作.现在,它为所有活动设置了首选项,解决该问题的简单方法是,在需要的地方初始化默认的SharedPreferences,在需要的地方初始化Custom SharedPreferences.

Technically these three lines will get the job done from one side. And now that it sets the Preference for all the activities, the simple way to overcome that is it initialize default SharedPreferences where needed and Custom SharedPreferences where are wanted.

getPreferenceManager().setSharedPreferencesName("custom_prefs");
getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
addPreferencesFromResource(R.xml.prefs);

这篇关于是否可以将Android“偏好设置/设置"屏幕配置为使用“默认"共享偏好设置以外的其他偏好设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆