【问题标题】:How to link to a specific preferencegroup如何链接到特定的偏好组
【发布时间】:2012-12-13 22:14:59
【问题描述】:

背景:
我有一个带有 3 个首选项组的preference.xml,分别称为 GroupA、GroupB 和 GroupC,每个组都有许多项目。我可以开始我的选项意图,它显示了我的 3 个组,单击每个组会给出子项。这工作得很好。

当我从我的主要活动开始偏好意图时,我可以传递一些额外的信息,如下所示。

Intent intent = new Intent(this, Options.class);
intent.putExtra("pg","GroupB");
startActivity(intent);

在 Options 类的 onCreate 例程中,读取额外的信息,如下所示:

String sPrefGroup = intent.getStringExtra("scr");

问题:
如何指示 Options 类显示选定的 PreferenceGroup,而不是从 Preferences XML 的根目录开始

【问题讨论】:

  • 我认为您可以删除另外两个组。类似这个答案:stackoverflow.com/a/4754139/857728
  • 是的,这是可能的,但是 GroupB 显示,我仍然需要单击它才能查看该组中的项目。我希望模拟偏好组的选择

标签: android sharedpreferences android-preferences preferenceactivity


【解决方案1】:

简单的解决方案是制作一个名为preferences_groupb.xml 的preferences.xml 文件的副本,其中只包含我想要显示的偏好项

然后在 Options.class 中我使用了类似于以下的代码:

Intent intent = getIntent(); 
String sPrefGroup = intent.getStringExtra("pgGroupB");
if (TextUtils.isEmpty(sPrefGroup))
    addPreferencesFromResource(R.xml.preference);
else
    addPreferencesFromResource(R.xml.preference_favorites);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多