如何在层次结构中向下启动首选项活动

发布于 2024-12-08 15:45:10 字数 261 浏览 4 评论 0原文

我正在 API 级别 9 上工作,因此无法使用新的首选项片段。

我有一个首选项活动,根据 api 演示从 xml 文件获取首选项。我有一个偏好屏幕层次结构,因此通常要获得偏好,您必须单击顶级屏幕。

除了从主菜单中获取所有首选项之外,我希望能够在其中一个子屏幕而不是顶部屏幕上启动首选项活动,这样我只需单击一下即可直接在其中一个子屏幕中启动它在与该组子偏好相关的活动中。

有人知道这是否可行吗?我认为有目的地传递一些数据是一种方法,但我找不到任何说明这是可能的。

I am working at API level 9 so can not use the new Preference Fragment stuff.

I have a preference activity getting the preferences from and xml file as per the api demos. I have a hierarchy of preference screens so normaly to get to a preference you have to click though a top level screen.

As well as getting to all the preferences from my main menu I would like to be able to start the preference activity at one of the sub screens rather than the top screen so I can start it directly in one of the sub screens on say a click in an activity that that group of sub preferences is relervant to.

Anyone know if this is posible? I would of thought that passing some data with the intent would be the way but I can not find anything saying this is possible.

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

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

发布评论

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

评论(2

迷途知返 2024-12-15 15:45:11

首先,您可以通过 android 兼容性包使用 Preference Fragment: http:// developer.android.com/sdk/compatibility-library.html

其次,是的,这是可能的。当您创建片段或活动(无论您决定)时,请使用 addPreferencesFromIntent() 方法( http://developer.android.com/reference/android/preference/PreferenceFragment.html#addPreferencesFromIntent%28android.content.Intent%29 ),而不是添加整个屏幕,只需添加您想要的子屏幕。

First of all, the Preference Fragment is available to you through the android compatibility package: http://developer.android.com/sdk/compatibility-library.html

Second, yes this is possible. When you're creating your fragment or acitvity (which ever you decide), use the addPreferencesFromIntent() method ( http://developer.android.com/reference/android/preference/PreferenceFragment.html#addPreferencesFromIntent%28android.content.Intent%29 ), and instead of adding the whole screen just add the subscreen you want.

请远离我 2024-12-15 15:45:10

好的,这就是我在柯蒂斯的帮助下得到的结果。

基本上,在我启动首选项活动的代码中,我对所有首选项没有任何操作,如果您只需要其中一些首选项,则需要执行操作。该操作需要与某种首选项或首选项组上的键匹配。

// all preferences
Intent launchPreferencesIntent = new Intent().setClass(this,
    PreferencesFromXml.class);
startActivity(launchPreferencesIntent);

// just key_trip_plot_control_preferences
Intent launchPreferencesIntent = new Intent(
    getString(R.string.key_trip_plot_control_preferences))
    .setClass(this, PreferencesFromXml.class);
startActivity(launchPreferencesIntent);

在我的 PreferencesFromXml 类中,我总是添加 xml 中的首选项,但如果我有一个操作,我会搜索首选项以查找匹配的键。如果我找到一个,我会删除所有首选项,然后添加匹配的首选项或其子项(如果它是 PreferenceGroupe)。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    String act = getIntent().getAction();

    if (act != null) {
        Preference res = findPreferenceByKey(getPreferenceScreen(), act);
        if (res != null) {
            getPreferenceScreen().removeAll();
            if (res instanceof PreferenceGroup) {
                PreferenceGroup groupe = (PreferenceGroup) res;
                // add sub items
                for (int i = 0; i < groupe.getPreferenceCount(); i++) {
                    Preference pref = groupe.getPreference(i);
                    if (pref != null) {
                        getPreferenceScreen().addPreference(pref);
                    }
                }
            } else { // just add the item.
                getPreferenceScreen().addPreference(res);
            }
        }
    }
}

protected Preference findPreferenceByKey(PreferenceGroup in, String key) {
    for (int i = 0; i < in.getPreferenceCount(); i++) {
        Preference pref = in.getPreference(i);
        if (pref == null) {
            // should not happen
            Log.v(TAG, "findPreferenceByKey null pref i:" + i);
            return null;
        } else if (pref.hasKey() && pref.getKey().equals(key)) {
            return pref;
        } else if (pref instanceof PreferenceGroup) {
            // recurse
            Preference res = findPreferenceByKey((PreferenceGroup) pref,
                    key);
            if (res != null) {
                return res;
            }
        }
    }
    return null;
}

OK here is what I have ended up with with Kurtis's help.

Basically in my code starting the Preferences activity I have no action for all the preferences and an action if you want just some of them. The action needs to match the key on a preference or preferenceGroupe of some sort.

// all preferences
Intent launchPreferencesIntent = new Intent().setClass(this,
    PreferencesFromXml.class);
startActivity(launchPreferencesIntent);

// just key_trip_plot_control_preferences
Intent launchPreferencesIntent = new Intent(
    getString(R.string.key_trip_plot_control_preferences))
    .setClass(this, PreferencesFromXml.class);
startActivity(launchPreferencesIntent);

In my PreferencesFromXml class I always add the preferences from the xml but then if I have an action I search though the preferences looking for a matching key. If I find one I removeAll preferences then add the matching one or it's children if it is a PreferenceGroupe back in.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    String act = getIntent().getAction();

    if (act != null) {
        Preference res = findPreferenceByKey(getPreferenceScreen(), act);
        if (res != null) {
            getPreferenceScreen().removeAll();
            if (res instanceof PreferenceGroup) {
                PreferenceGroup groupe = (PreferenceGroup) res;
                // add sub items
                for (int i = 0; i < groupe.getPreferenceCount(); i++) {
                    Preference pref = groupe.getPreference(i);
                    if (pref != null) {
                        getPreferenceScreen().addPreference(pref);
                    }
                }
            } else { // just add the item.
                getPreferenceScreen().addPreference(res);
            }
        }
    }
}

protected Preference findPreferenceByKey(PreferenceGroup in, String key) {
    for (int i = 0; i < in.getPreferenceCount(); i++) {
        Preference pref = in.getPreference(i);
        if (pref == null) {
            // should not happen
            Log.v(TAG, "findPreferenceByKey null pref i:" + i);
            return null;
        } else if (pref.hasKey() && pref.getKey().equals(key)) {
            return pref;
        } else if (pref instanceof PreferenceGroup) {
            // recurse
            Preference res = findPreferenceByKey((PreferenceGroup) pref,
                    key);
            if (res != null) {
                return res;
            }
        }
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文