【问题标题】:Android WebView: how to make context menu work for text area?Android WebView:如何使上下文菜单适用于文本区域?
【发布时间】: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


    【解决方案1】:

    尝试使用 WebView 注册 onLongClickListener。视图可能正在使用该事件,而不是将其传递回 Activity。我假设当前工作的菜单是通过使用 Activity.registerForContextMenu() 注册 WebView 来创建的?

    【讨论】:

    • 非常感谢您的回答。其实我也试过了,也没打到。是的,我的上下文菜单是通过 Activity.registerForContextMenu(webView) 创建的。
    猜你喜欢
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    相关资源
    最近更新 更多