如何添加“长按监听器”偏好?

发布于 2024-12-27 19:07:52 字数 176 浏览 0 评论 0原文

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

长按时,这些将执行某些操作,但是 OnPreferenceClickListener 仅支持正常点击,不支持长按。

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

谢谢

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

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨与橙与城 2025-01-03 19:07:52

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

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

PreferenceActivity 实际上是一个 ListActivity,在幕后有一个特殊的适配器。通常的(不长的)点击是通过使用通常的 ListView 机制 setOnItemClickListener 来处理的。设置此功能的代码位于 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 子类化并重写绑定以将长项目单击侦听器添加到列表视图中非常容易,但此类是最终类。因此,我最终将以下代码添加到我的 PreferenceActivity 子类中:

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 子类,长按时会自动调用该子类:

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

In the event that the link dies, here is the main body of the post at that link. Note: I did not author anything below.

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

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

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;
    }
}
追星践月 2025-01-03 19:07:52

目前无法为您进行测试,但我想知道您是否可以通过在 Preference 上使用 getView() 方法来实现此目的。然后,一旦您拥有该View,请使用setOnLongClickListener()

Unable to test for you at the moment, but I'm wondering if you could achieve this by using the getView() method on a Preference. Then, once you have that View, use setOnLongClickListener().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文