首选项点击监听器上的首选项活动 [英] Preference Activity on Preference Click Listener

查看:113
本文介绍了首选项点击监听器上的首选项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个首选项活动",列表中的大多数首选项将在执行代码,而不是直接修改SharedPreference.我的preferences.xml文件如下所示.

I am building a Preference Activity where most of the preferences in the list will be executing code and not modifying a SharedPreference directly. My preferences.xml file looks like this.

<PreferenceCategory
    android:title="Connection" >

    <Preference
        android:id="@+id/settings_connectToNewComputer"
        android:key="connectToNewComputer"
        android:summary="Currently connected to:"
        android:title="Connect to new computer" />

    <Preference
        android:id="@+id/removeDevice"
        android:key="removeDevice"
        android:summary="Remove this device from the computer's whitelist"
        android:title="Remove this device from computer" />

</PreferenceCategory>

<PreferenceCategory
    android:title="About" >

    <Preference
        android:id="@+id/settings_About"
        android:key="about"
        android:summary="About me and my thanks to those who made this app great"
        android:title="About Hue Pro" />

    <Preference
        android:id="@+id/contact"
        android:key="contact"
        android:summary="Contact me with comments, bugs, and suggestions for updates"
        android:title="Contact me" />

</PreferenceCategory>

我的目标是单击这些首选项之一时执行一段代码.类似于Google Play设置首选项菜单中的清除搜索历史记录". ( http://i.imgur.com/qnHbJX9.png )

My goal is to have a block of code executed when a one of these preferences are clicked. Similar to the "Clear search history" in the Google Play settings preference menu. (http://i.imgur.com/qnHbJX9.png)

有人知道如何做到这一点吗?

Does anyone know how to make this possible?

我必须补充一点,我尝试使用findPreference("KeyNameHere"),但它始终返回null.

I have to add that I have tried using findPreference("KeyNameHere") but it always returns null.

谢谢!

我在这段代码中添加并实现了OnPreferenceClickListener:

I added in this code and implemented OnPreferenceClickListener:

@Override
public boolean onPreferenceClick(Preference preference) {
    return false;
}

但是永远不会调用此方法.还有另一种方法吗?

But this method never gets called. Is there another way to do this?

我发现,如果我删除了PreferenceCategory标签,那么我将得到以下信息:

I have found that if I take out the PreferenceCategory tags so I am left with this:

<Preference
    android:id="@+id/settings_connectToNewComputer"
    android:key="connectToNewComputer"
    android:summary="Currently connected to:"
    android:title="Connect to new computer" />

<Preference
    android:id="@+id/removeDevice"
    android:key="removeDevice"
    android:summary="Remove this device from the computer's whitelist"
    android:title="Remove this device from computer" />

<Preference
    android:id="@+id/settings_About"
    android:key="about"
    android:summary="About me and my thanks to those who made this app great"
    android:title="About Hue Pro" />

<Preference
    android:id="@+id/contact"
    android:key="contact"
    android:summary="Contact me with comments, bugs, and suggestions for updates"
    android:title="Contact me" />

并称呼它:

getPreferenceScreen().setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            return false;
        }
    });

然后我实际上收到了click事件的响应.唯一的缺点是我必须删除首选项分组.任何人都知道为什么会这样,并且有任何解决办法吗?

then I actually get a response from the click event. The only down side is I have to remove the preference grouping. Anyone know why this is and any way to fix it?

推荐答案

我想出了自己的解决方案(我认为是真的弄糟了).但是可以.

I came up with my own (what I believe is really messed up) solution; but it works.

for(int x = 0; x < getPreferenceScreen().getPreferenceCount(); x++){
        PreferenceCategory lol = (PreferenceCategory) getPreferenceScreen().getPreference(x);
        for(int y = 0; y < lol.getPreferenceCount(); y++){
            Preference pref = lol.getPreference(y);
            pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }

            });
        }
    }

因此,我了解到有一个分层系统,其工作方式如下:PreferenceScreen具有子项PreferenceCategory具有子项Preference,正如您在XML文件中看到的那样.我的问题是我无法直接从PreferenceScreen设置首选项的onClickListeners.因此,我做了两个for循环,这些循环将涉及到每个Preference并为每个Preference设置一个OnPreferenceClickListener.凌乱,但终于奏效了.

So what I have learned is there is a hierarchical system that works like: PreferenceScreen has children PreferenceCategory has children Preference, as you can see in the XML file. My problem was I could not set the preferences' onClickListeners directly from the PreferenceScreen. So I made two for loops that will get down to each Preference and set an OnPreferenceClickListener for each and every one of them. Messy, but works finally.

这篇关于首选项点击监听器上的首选项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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