大厦preference屏幕code取决于其他设置 [英] Building Preference screen in code depending on another setting

查看:206
本文介绍了大厦preference屏幕code取决于其他设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里搜查,看样品,但还没有找到一个答案什么本质上是一个简单的问题。根据在preceding列表preference作出的选择,我想建立的CheckBox preferences的preference屏幕动态在code,然后显示,当我点击了一个简单preference一个点击监听器。复选框列表和数量将在每种情况下不同的

I have searched here and looked at samples but haven't yet found an answer to what is essentially a simple problem. Depending on the choice made in a preceding ListPreference, I want to build a preference screen of CheckBoxPreferences dynamically in code, which is then shown when I click on a simple preference with a click listener. The list and number of check boxes will be different in each case.

这就是我有这么远 - 只要code的简单一点,以测试在onclick监听器的概念,但如何让复选框preference屏幕出现?必须有一个简单的解释,为什么它没有。我在做什么错了?

This is where I have got to so far - just a simple bit of code to test the concept in the onClick listener, but how to get the check box preference screen to appear? There must be a simple explanation why it doesn't. What am I doing wrong?

我的XML code的部分:

Part of my xml code:

<PreferenceCategory android:title="Filters">
    <PreferenceScreen android:key="FilterScreen"
        android:title="Filters" android:summary="Click to change filter settings">
        <ListPreference android:title="Filter type" 
            android:summary="Set to: Gliding"
            android:key="filterType"
            android:defaultValue="0"
            android:entries="@array/filterTypeOptions"
            android:entryValues="@array/filterTypeValues" />
        <CheckBoxPreference android:title=""
            android:summary="Include Aerodrome Notams"
            android:defaultValue="false" android:key="filterIncludeAerodrome" />
        <CheckBoxPreference android:title=""
            android:summary="Delete night-time Notams"
            android:defaultValue="true" android:key="filterDeleteNighttime" />
        <ListPreference android:title="Select category to change" 
            android:summary="Set to: Airspace organisation"
            android:key="filterCategory"
            android:defaultValue="0"
            android:entries="@array/filterCategoryOptions"
            android:entryValues="@array/filterCategoryValues" />
        <Preference android:title="Show filters for category"
            android:summary="Click to choose subjects to delete"
            android:key="filterShow" />
    </PreferenceScreen>
</PreferenceCategory>

中显示过滤器类的内容将取决于过滤器类型和选择类别以更改的设置。

The contents of "Show filters for category" will depend on the "Filter type" and "Select category to change" settings.

这是一个简单的测试code我有一个显示过滤器,单击监听器(只削减到显示要领):

This is the simple test code I have for the "Show filters" click listener (cut down just to show essentials):

public class Settings extends PreferenceActivity
                  implements OnSharedPreferenceChangeListener
{
    ------
    public static final String KEY_FILTER_SHOW = "filterShow";

    ------
    private Preference mFilterShow;

    ------
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings);

        // Get a reference to the preferences

        ------
        mFilterShow = (Preference)findPreference(KEY_FILTER_SHOW);

        ------
        // Set the click listener for  Show Filter options
        mFilterShow.setOnPreferenceClickListener(new OnPreferenceClickListener()
        {
            public boolean onPreferenceClick(Preference pref)
            {
                Context ctx = pref.getContext();
                PreferenceScreen screen =
                    pref.getPreferenceManager().createPreferenceScreen(ctx);
                CheckBoxPreference cb1 = new CheckBoxPreference(ctx);
                cb1.setTitle("This is cb1");
                cb1.setKey("cb1_key");
                cb1.setDefaultValue(false);
                screen.addPreference(cb1);
                return true;
            }

        });

我相信,必须有一个简单的答案是我做错了,还是什么我已经错过了!请帮助...

I am sure that there must be a simple answer to what I have done wrong, or what I've missed! Please help...

推荐答案

OK,我已经解决了自己的问题,经过反复的过程!其他人可能会发现这很有用。

OK, I have solved the problem myself, through a process of iteration! Others might find this useful.

只要创建XML中的一个空preferenceScreen:

Just create an empty PreferenceScreen in the xml:

        <PreferenceScreen android:title="Show filters for category"
            android:summary="Click to choose subjects to delete"
            android:key="filterShow">
        </PreferenceScreen>

然后在code没有必要对的onClick听者 - 在屏幕上的内容是在的onCreate函数创建。实际上,由于屏幕的内容需要改变时,在分类列表preference做出的选择(见原code)的变化,这需要在一个单独的功能,这是从的onCreate和onShared $叫都去p $ pferenceChanged:

Then in the code there is no need for the onClick listener - the contents of the screen are created in the onCreate function. Actually, since the contents of the screen need to change when the choice made in the Category list preference (see original code) changes, this needs to go in a separate function which is called both from onCreate and onSharedPreferenceChanged:

public static final String KEY_FILTER_SHOW = "filterShow";
...    
private PreferenceScreen mFilterShow;
...

// In onCreate:

    // Get a reference to the PreferenceScreen
    mFilterShow =
        (PreferenceScreen)getPreferenceScreen().findPreference(KEY_FILTER_SHOW);

    // Now the code to create the contents of the screen
    mFilterShow.removeAll();
    CheckBoxPreference cb1 = new CheckBoxPreference(this);
    cb1.setTitle("This is cb1");
    cb1.setKey("cb1_key");
    cb1.setDefaultValue(true);
    mFilterShow.addPreference(cb1);

以上仅仅是概念证明。它的工作原理完全一样,你期望的那样。在我的最终版本,我将创建的CheckBox preferences数组与'新'开始,然后为每个类别选择的设置时,屏幕上的内容,因为它改变了重新使用它们(改变标题和默认值)。的可以是用于每个类别不同需要复选框数 - 我将创建所需的最大数量的阵列,然后添加多达我需要在每种情况下

The above is just "proof of concept". It works exactly as you would expect. In my final version, I will create an array of CheckBoxPreferences with 'new' initially, then re-use them (changing title and default) when setting up the contents of the screen for each Category choice as it changes. The number of check boxes required may be different for each category - I will create an array for the maximum number required, then add as many as I need in each case.

这篇关于大厦preference屏幕code取决于其他设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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