首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用自定义上下文操作栏选择WebView文本

使用自定义上下文操作栏选择WebView文本
EN

Stack Overflow用户
提问于 2014-03-12 05:22:30
回答 1查看 22.4K关注 0票数 24

我已经使用this guide from Googlethis tutorial生成了我自己的上下文操作栏。

代码语言:javascript
复制
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.annotation_menu, menu);
        return true;
    }

    // Called each time the action mode is shown.
    // Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.custom_button:
                // do some stuff
                break;
            case R.id.custom_button2:
                // do some other stuff
                break;
            default:
                // This essentially acts as a catch statement
                // If none of the other cases are true, return false
                // because the action was not handled
                return false;
        }
        finish(); // An action was handled, so close the CAB
        return true;
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};

此菜单设计为在用户选择文本时显示,因此它会覆盖本机的复制/粘贴菜单。现在我开始讨论我的问题了。

因为我覆盖了用于文本选择的函数,所以我还向WebView添加了一个LongClickListener并实现了onLongClick(View v)方法,这样我就可以检测用户何时做出选择。

代码语言:javascript
复制
    myWebView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if (mActionMode != null) {
                return false;
            }

            mActionMode = startActionMode(mActionModeCallback);
            v.setSelected(true);
            return true;
        }
    });

当我长点击时,我看到我的自定义菜单出现,但没有文本突出显示。

我需要有文本选择功能;没有它,我的菜单就没有意义了。

如何覆盖onLongClick(View v),同时保持Android提供的文本选择?

如果这是不可能的,我是否可以调用其他地方的startActionMode(mActionModeCallback),这样文本将被正常选择,但我的自定义菜单也会出现?

如果这两个都不可能...帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-03-12 07:27:44

我做类似的事情的方式是只覆盖onTouchListener,并调用GestureDetector来检测WebView何时被长时间按下,并从那里执行我想要的操作。下面是一些示例代码,它允许您在不牺牲WebView中的文本选择的情况下捕获长按事件。希望这能有所帮助。

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    WebView mWebView = (WebView) findViewById(R.id.myWebView);
    GestureDetector mGestureDetector = new GestureDetector(this, new CustomGestureListener());
    mWebView.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View view, MotionEvent arg1) {

            //Suggestion #1 - this just lets the touch to be handled by the system but allows you to detect long presses
            mGestureDetector.onTouchEvent(arg1);
            return false;

            //Suggestion #2 - this code will only let the touch be handled by the system if you don't detect a long press
            return mGestureDetector.onTouchEvent(arg1);
        }
    });
}

private class CustomGestureListener extends SimpleOnGestureListener {

    @Override
    public void onLongPress(MotionEvent e) {
        //do stuff
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22336903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档