Android OnLongClickListener 奇怪/不可靠的行为
我目前正在与 Android Api Lvl 8 上的 OnLongClickListener 进行斗争。
使用这段代码:
this.webView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
System.out.println("long click");
return true;
}
});
它工作得很好。我可以按 WebView 上的任意位置,并且每次都会触发事件。
现在看一下这个:
this.webView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final EditText editText = getUrlTextField();
switch (editText.getVisibility()) {
case View.VISIBLE:
editText.setVisibility(View.GONE);
return true;
case View.GONE:
editText.setVisibility(View.VISIBLE);
return true;
default:
return false;
}
}
});
假设 URL EditText 组件当前可见,它会从显示中消失,并在触发另一个长单击事件时再次显示。 但是,如果您运行此命令,当长按 WebView 上的任何位置时,该事件只会运行一次(!)。让事情变得复杂的是,当在网站上的链接上执行长按时,长按会再次起作用...
任何人都可以解释这是否是 sdk 中的错误和/或我对 OnLongClickListener 的工作方式的思考是否存在错误?!? :/
编辑:
我现在在 Nexus One 上运行了几种不同的场景,并得出以下结论:更改运行时的布局或多或少会杀死 OnLongClickListener...我还没有找到一种方法为了让它可靠地工作...
如果有人能给我一个提示,我真的很感激...我已经束手无策了:(
I'm currently fighting against the OnLongClickListener on Android Api Lvl 8.
Take this code:
this.webView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
System.out.println("long click");
return true;
}
});
It works perfectly. I can press anywhere on the WebView and the event triggers every time.
Now take a look at this one:
this.webView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final EditText editText = getUrlTextField();
switch (editText.getVisibility()) {
case View.VISIBLE:
editText.setVisibility(View.GONE);
return true;
case View.GONE:
editText.setVisibility(View.VISIBLE);
return true;
default:
return false;
}
}
});
Assuming the URL EditText components is currently visible, it gets gone from the display and should be shown again when another long click event is triggered.
But if you run this, the event just works once (!) when one performs a long click on any position on the WebView. To make things complicated, the long click works again when it is performed on a link on the website...
Can anyone explain if it is a bug in the sdk and/or if there is a mistake in my thinking how the OnLongClickListener is working?!? :/
EDIT:
I've run now several different scenarios on a Nexus One and come to following conclussion: Changing the layout on runtime more or less kills the OnLongClickListener... I haven't found a way to get it work reliably at all...
I would really appreciate if anyone could give me a hint... I'm at my wits end :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我最终在每次重新布局后重新设置侦听器。
Personnally, I ended up by re-setting the listener after each relayout.
我也遇到过这个问题。看来,如果视图布局以需要修改子视图边界的方式发生变化(即 TextView 是 wrap_content 宽度,并且您将其文本设置为比以前更长/更短),则层次结构中的视图将具有其
onStartTemporaryDetach
方法被调用(很可能是由于布局过程,尽管我还没有深入挖掘以确定)。如果您查看View
的源代码,onStartTemporaryDetach
最终会取消设置视图的按下状态。更改布局中将定期更新的视图,使其具有无论您设置的值如何都不会更改的边界,将解决该问题。尽管如此,这仍然不太好。
I've run into this issue as well. It seems that if the view layout changes in a way that child view bounds need to be modified (i.e. TextView is wrap_content width and you set its text to something longer/shorter than it was before), views in the hierarchy will have their
onStartTemporaryDetach
method called (most likely due to a layout pass, although I haven't dug deep enough to find out for sure). If you look at the source forView
thatonStartTemporaryDetach
ultimately unsets the pressed state of the view.Changing the views in your layout that will be updated periodically to have bounds that will not change regardless of the value you set, will fix the issue. Although, that is still not awesome.