Android 应用程序中每个视图的单独首选项

发布于 2024-10-12 22:24:11 字数 190 浏览 8 评论 0原文

当应用程序运行时,我有多个视图来来去去。我希望每个视图都有自己的个人偏好,这些偏好存储为视图的 ID 标签。在这些之上是“常规首选项”,子首选项在创建视图时引用它来获取其默认值。

现在我已将常规首选项设置为默认的共享首选项。但我不知道如何创建新的首选项并设置活动 UI 以便用户可以更改它们。它与设置 SharedPreferences 几乎相同吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相思故 2024-10-19 22:24:11

这可能不完全是您所要求的,但这就是我所做的:

在我的主要活动中,当我调用首选项活动时,我将自定义首选项文件的名称作为意图中的额外数据传递给它:

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);

然后,在我的首选项活动中,我获取自定义首选项名称并将其设置如下:

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);
}

最后,在我的主要活动中,我获得如下所示的特定首选项:

SharedPreferences preferences = getSharedPreferences(preferencesName, MODE_PRIVATE);
String somePreference = preferences.getString("somePreference", defaultValue);

this may not be exactly what you're asking for, but here's what I do:

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);

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);
}

lastly, in my main activity, I get specific preferences like this:

SharedPreferences preferences = getSharedPreferences(preferencesName, MODE_PRIVATE);
String somePreference = preferences.getString("somePreference", defaultValue);
甜宝宝 2024-10-19 22:24:11

不知怎的,我不值得评论,但值得写一个答案,所以我们开始吧:
我真的很想知道如何将sharedPreferences与PreferencesActivity一起使用而不是DefaultSharedPreferences。

我能想到的实现此目的的一种方法是让preferenceActivity 将值保存到defaultSharedPreferences,然后读取这些值并将它们保存到与所保存值的类型相匹配的名称关联的sharedPreferences 中。

但这似乎是非常错误的。那么你们是怎么做到的呢?或者您是否将任何 PreferencesActivties 中的所有值保存到 defaultSharedPreferences 中?

Somehow I am not worthy to comment but to write an answer, so here we go:
I'd really like to know how to use sharedPreferences with PreferencesActivity instead of DefaultSharedPreferences.

One way I can think of to accomplish this is letting the preferenceActivity save the values to defaultSharedPreferences and then read these values out and save them into a sharedPreferences associated with a name that would match the kind of values saved.

But this seems very wrong. So how do you guys do this? Or do you save all your values from any PreferencesActivties into defaultSharedPreferences?

太阳哥哥 2024-10-19 22:24:11

您可以使用 PreferenceManager 来实现该目标。

You can use PreferenceManager to achieve the objective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文