添加听众单独的列表preference项目 [英] Adding listeners to individual ListPreference items

查看:193
本文介绍了添加听众单独的列表preference项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想个别的收听者在目录preference添加到项目,但我就是无法找到合适的code去做。

例如,假设我有一个应用程序需要的地区的需要设置的地方。所以我有一个目录preference 三个选项;美洲,亚洲,欧洲。

当我使用触控板通过项目滚动我想他们时,他们获得焦点的话语preference的文本。

我敢肯定在某个地方API中,我可以一个监听器添加到单个项目,但我无法找到它。
有一个在目录preference一个西顿preferenceClickListener(..)(从<$继承C $ C>对话框preference ),但是这是当你选择实际父preference项目的。

我是否需要写一个特殊的子类?

修改

寻找到这种更我想后,我需要进一步澄清我的问题。
当使用preferences可以覆盖上preferenceTreeClick(preferenceScreen prefScreen,preference preF)方法在 preferenceActivity 。这使您可以拦截在preferences任何点击。不过,我想一个侦听器添加到弹出的对话框中的实际。从上面的例子中的美洲,亚洲,欧洲选项。所以,如果我选择或点击亚洲我可以拦截它。

所以我的(精)的问题是如何能侦听器添加到这些个别选项。

欢呼声


解决方案

 最终名单preference名单=(名单preference)preference;
        lista.setOn preferenceChangeListener(新在preferenceChangeListener(){
            在preferenceChange公共布尔(preference preference,对象为newValue){                INT指数= list.findIndexOfValue(newValue.toString());
                如果(索引!= -1)
                {
                    Toast.makeText(getBaseContext(),list.getEntries()[指数],Toast.LENGTH_LONG).show();
                }
                返回true;
            }
        });

I'm trying to add individual listeners to items in a ListPreference but I just can't find the right code to do it.

For example, assume I have an app where needs the region to be set. So I have a ListPreference with three options; Americas, Asia, Europe.

When I use the trackpad to scroll through the items I would like them to speak the text of the preference when they get focus.

I'm sure somewhere in the API I can add a listener to the individual items but I just can not find it. There is a setOnPreferenceClickListener(..) method in ListPreference (inherited from DialogPreference) but that is for when you select the actual parent preference item.

Do I need to write a special subclass?

EDIT

After looking into this more I think I need to further clarify my question. When using preferences you can override the onPreferenceTreeClick (PreferenceScreen prefScreen, Preference pref) method in the PreferenceActivity. This allows you to intercept any clicks on preferences. However I want to add a listener to the actual dialog that pops up. The "Americas, Asia, Europe" options from the above example. So if I select or click on Asia I can intercept it.

So my (refined) question is how can add a listener to those individual options.

cheers

解决方案

final ListPreference list = (ListPreference) preference;
        lista.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                int index = list.findIndexOfValue(newValue.toString());
                if (index != -1) 
                {
                    Toast.makeText(getBaseContext(), list.getEntries()[index], Toast.LENGTH_LONG).show();
                }
                return true;
            }
        });

这篇关于添加听众单独的列表preference项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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