独立preferences每个视图在一个Android应用程序 [英] Separate preferences for each view in an Android app
问题描述
我来来去去在应用程序运行多个视图。我想每个视图具有存储作为视图的ID标签自身的个人preferences。以上这些是常规preferences,一个视图被创建时,子preFS参考得到他们的默认值。
I have multiple views that come and go as the application runs. I want each view to have its own personal preferences that are stored as the ID tag of the view. Above these is the "General Preferences" that the sub prefs reference to get their default values when a view it is created.
现在我有它设置一个总preferences是默认的共享preferences。但我不知道如何创建新的preferences并成立了一个活动的用户界面,使用户可以更改它们。它是pretty大致相同设置共享preferences?
Right now I have it set up that the General Preferences are the default SharedPreferences. But I have no Idea how to create the new preferences and set up an activity UI so the user can change them. Is it pretty much the same as setting up the SharedPreferences?
推荐答案
这可能不是你问什么了,但这里是我做的:
this may not be exactly what you're asking for, but here's what I do:
在我的主要活动,当我打电话了preferences活动,我将它传递定制preference文件在意向额外数据的名称:
in my main activity, when I call the preferences activity, I pass it the name of the custom preference file as extra data in the intent:
static final String EXTRA_PREFERENCES_NAME = "android.intent.extra.PREFERENCES_NAME";
...
Intent intent = new Intent(this, Preferences.class);
intent.putExtra(EXTRA_PREFERENCES_NAME, preferencesName);
startActivity(intent);
然后,在我的preferences活动中,我得到的定制preferences名称并设置它像这样:
then, in my preferences activity, I get the custom preferences name and set it like this:
public class Preferences extends PreferenceActivity {
private String preferencesName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the custom preferences name from the extra data in the intent
preferencesName = getIntent().getExtras().getString(MainActivity.EXTRA_PREFERENCES_NAME);
// set the preferences file name
getPreferenceManager().setSharedPreferencesName(preferencesName);
// get the default preferences from XML
addPreferencesFromResource(R.xml.preferences);
}
最后,在我的主要活动中,我得到具体的preferences是这样的:
lastly, in my main activity, I get specific preferences like this:
SharedPreferences preferences = getSharedPreferences(preferencesName, MODE_PRIVATE);
String somePreference = preferences.getString("somePreference", defaultValue);
这篇关于独立preferences每个视图在一个Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!