如何添加“长按监听器”偏好?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果链接失效,这里是该链接的帖子正文。注意:我没有编写下面的任何内容。
内置的 Preference 类有一个接收点击的方法 onClick,但没有接收长点击的方法。在我现在的项目中,其实也有这个需求,并且找到了实现的方法。
PreferenceActivity 实际上是一个 ListActivity,在幕后有一个特殊的适配器。通常的(不长的)点击是通过使用通常的 ListView 机制 setOnItemClickListener 来处理的。设置此功能的代码位于 PreferenceScreen 中:
将 PreferenceScreen 子类化并重写绑定以将长项目单击侦听器添加到列表视图中非常容易,但此类是最终类。因此,我最终将以下代码添加到我的 PreferenceActivity 子类中:
现在我可以拥有一个实现 View.OnLongClickListener 的 Preference 子类,长按时会自动调用该子类:
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:
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:
Now I can have a Preference subclass that implements View.OnLongClickListener, which is automatically invoked for long clicks:
目前无法为您进行测试,但我想知道您是否可以通过在
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 aPreference
. Then, once you have thatView
, usesetOnLongClickListener()
.