如何启动preferenceActivity一部分一路下滑层次 [英] How to start preferenceActivity part way down the hierarchy

查看:261
本文介绍了如何启动preferenceActivity一部分一路下滑层次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在API级9个工作所以不能使用新的preference片段的东西。

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

我有一个preference活动得到从preferences和XML文件按照API演示。我有preference屏幕让normaly的层次去你必须点击虽然顶级屏幕preference。

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.

,以及获得从我的主菜单中的所有preferences我希望能够启动preference活动在子屏幕而不是顶部的屏幕之一,所以我可以直接启动上说,在一个活动,该集团子preferences是relervant了点击子屏幕之一。

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.

推荐答案

确定这里是我已经结束了与裴家的帮助。

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

基本上在我的code启动preferences活动我对所有的preferences没有行动和行动,如果你想只是其中的一部分。该行动需要与某种在preference键或preferenceGroupe。

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

在我的preferencesFromXml类,我总是从XML加preferences但如果我有一个动作我搜索虽然preferences寻找一个匹配的密钥。如果我找到一个我removeall过preferences然后添加匹配一个或它的孩子如果是preferenceGroupe回。

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

这篇关于如何启动preferenceActivity一部分一路下滑层次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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