【发布时间】:2011-11-15 22:01:16
【问题描述】:
将上下文菜单添加到 web 视图很简单。但是,默认情况下,仅当您单击链接或图像时才会弹出菜单,而不是常规文本区域。
我发现这个One 建议将 onLongClick 添加到活动中,但我尝试了它并没有被击中。
//--------------------------------------------- ------------------
编辑:
我从一个相关问题中得到提示,我必须将 webview 子类化并覆盖一些函数。我似乎让上下文菜单正常工作。但是,webview 的行为不正常。特别是,我不能再滚动视图了。我想我只是想念一些东西。非常感谢您的帮助。这是我的代码:
public class MyWebView extends WebView {
WebViewerActivity _activity;
Context _context;
GestureDetector _gd;
public MyWebView(Context context, AttributeSet attributeSet){
super(context, attributeSet);
this._context = context;
_gd = new GestureDetector(context, sogl);
}
public void setActivity(WebViewerActivity activity){
_activity = activity;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return _gd.onTouchEvent(ev);
}
GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener(){
public boolean onDown(MotionEvent event)
{
return true;
}
public void onLongPress(MotionEvent event)
{
_activity.onLongClick(MyWebView.this);
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
MyWebView.this.scrollBy((int)distanceX, (int)distanceY);
return true;
}
};
}
//--------------------------------------------- ------------------
更多编辑:
好的,如上所示,我将 onScroll 覆盖添加到 SimpleOnGestureListener,它使 webview 滚动工作。但是,如果您将其滚动到外部太远,则 webview 的原始滚动会自动反弹。但是,这种滚动没有这个好功能。知道如何解决这个问题吗?
【问题讨论】:
标签: android webview contextmenu