本文介绍了如何添加“长按侦听器"偏好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PreferenceActivity,我想在其中动态添加首选项.

I have an PreferenceActivity where I would like to add Preferences dynamically.

在长按时,这些会做一些事情,但是 OnPreferenceClickListener 只支持正常点击,不支持长点击.

On a long click, these shall do something, however OnPreferenceClickListener only supports normal clicks, no long clicks.

有没有办法实现这个功能,我错过了什么?

Is there a way to implement this feature, did I miss something?

谢谢

推荐答案

如果链接失效,这里是该链接的帖子正文.注意:我没有在下面创作任何内容.

内置的Preference类有接收点击的方法onClick,但是没有接收长点击的方法.在我目前的项目中,我其实有这个需求,并且找到了实现的方法.

The built-in Preference class has a method to receive clicks, onClick, but no method to receive long clicks. In my current project, I actually have a need for this, and found a way to implement it.

PreferenceActivity 实际上是一个 ListActivity,在幕后有一个特殊的适配器.通常的(不长的)点击是通过使用通常的 ListView 机制 setOnItemClickListener 来处理的.设置它的代码在 PreferenceScreen 中:

PreferenceActivity is actually a ListActivity, with a special adapter behind the scenes. The usual (not long) clicks are processed by using the usual ListView mechanism, setOnItemClickListener. The code to set this up is in PreferenceScreen:

public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener.... {
    public void bind(ListView listView) {
        listView.setOnItemClickListener(this);
        listView.setAdapter(getRootAdapter());

        onAttachedToActivity();
    }

    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Object item = getRootAdapter().getItem(position);
        if (!(item instanceof Preference)) return;

        final Preference preference = (Preference) item;
        preference.performClick(this);
    }
}

将 PreferenceScreen 子类化并覆盖 bind 以向列表视图添加长项单击侦听器真的很容易,除非此类是最终的.因此,我最终将以下代码添加到我的 PreferenceActivity 子类中:

It would be really easy to subclass PreferenceScreen and override bind to add a long-item-click listener to the list view, except this class is final. Because of this, I ended up adding the following code into my PreferenceActivity subclass:

public class BlahBlahActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.account_options_prefs);

        ListView listView = getListView();
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listView = (ListView) parent;
                ListAdapter listAdapter = listView.getAdapter();
                Object obj = listAdapter.getItem(position);
                if (obj != null && obj instanceof View.OnLongClickListener) {
                    View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
                    return longListener.onLongClick(view);
                }
                return false;
            }
        });
    }
}

现在我可以有一个实现了 View.OnLongClickListener 的 Preference 子类,它会在长按时自动调用:

Now I can have a Preference subclass that implements View.OnLongClickListener, which is automatically invoked for long clicks:

public class BlahBlahPreference extends CheckBoxPreference implements View.OnLongClickListener {
    @Override
    public boolean onLongClick(View v) {
        // Do something for long click
        return true;
    }
}

这篇关于如何添加“长按侦听器"偏好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:10