如何在 WebView 文本选择菜单中保留标准的“全选”和“复制”菜单项?

Posted

技术标签:

【中文标题】如何在 WebView 文本选择菜单中保留标准的“全选”和“复制”菜单项?【英文标题】:How do I keep standard 'select all' and 'copy' menu items in the WebView text selection menu? 【发布时间】:2019-11-09 08:13:01 【问题描述】:

我需要使用我的自定义命令扩展 android WebView 的文本选择菜单。 到目前为止,我已经设法通过以下方式实现了我的自定义 webview 文本选择菜单:

public class CustomActionWebView extends WebView 

    static String TAG = "CustomActionWebView";

    ActionMode mActionMode;

    int mMenuID;
    Activity mParentAct;

    ActionSelectListener mActionSelectListener;
    ButtonClickListener mButtonClick;

    public CustomActionWebView(Context context) 
        super(context);
    

    public CustomActionWebView(Context context, AttributeSet attrs) 
        super(context, attrs);
    

    public CustomActionWebView(Context context, AttributeSet attrs, int defStyleAttr) 
        super(context, attrs, defStyleAttr);
    

    public void setActionMenu(int mActionMenu, Activity ParentAct) 

        setWebViewClient(new CustomWebViewClient());
        this.mMenuID = mActionMenu;
        mParentAct = ParentAct;
    

    private ActionMode resolveActionMode(ActionMode actionMode) 
        if (actionMode != null) 
            final Menu menu = actionMode.getMenu();
            mActionMode = actionMode;
            menu.clear();

            mParentAct.getMenuInflater().inflate(mMenuID, menu);

            for (int i = 0; i < menu.size(); i++) 
                MenuItem mi = menu.getItem(i);

                mi.setOnMenuItemClickListener(item -> 
                    int id=item.getItemId();
                    if(id == R.id.cab_select_all)
                    
                        String js = "var el =document.getElementsByTagName(\"BODY\")[0]; el.setSelectionRange(0, el.value.length); alert(el.value);)";
                        evaluatejavascript("javascript:" + js, null);
                    
                    else getSelectedData( item.getItemId());
                    releaseAction();
                    return true;
                );

            

        
        mActionMode = actionMode;
        return actionMode;
    

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback) 
        ActionMode actionMode = super.startActionMode(callback);
        return resolveActionMode(actionMode);
    

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback, int type) 
        ActionMode actionMode = super.startActionMode(callback, type);
        return resolveActionMode(actionMode);
    

    private void releaseAction() 
        if (mActionMode != null) 
            mActionMode.finish();
            mActionMode = null;
        
    

    private void getSelectedData(int menuID) 


        String js2 = "(function getSelectedText() " +
                "var txt;" +
                "var parentID;" +
                "var test;" +
                "var menuID = " + menuID + ";" +
                "if (window.getSelection) " +
                "txt = window.getSelection().toString();" +
                "parentID = window.getSelection().baseNode.parentNode.id;"+
                "test = '1st';"+
                " else if (window.document.getSelection) " +
                "txt = window.document.getSelection().toString();" +
                "parentID = window.document.getSelection().baseNode.parentNode.id;"+
                "test = '2nd';"+
                " else if (window.document.selection) " +
                "txt = window.document.selection.createRange().text;" +
                "parentID = window.document.selection.baseNode.parentNode.id;"+
                "test = '3rd';"+
                "" +
                "JSInterface.callback(txt, menuID, parentID);" +
                ")()";


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
            evaluateJavascript("javascript:" + js2, null);
         else 
            loadUrl("javascript:" + js2);
        
    

    public void linkJSInterface() 
        addJavascriptInterface(new ActionSelectInterface(this), "JSInterface");
        addJavascriptInterface(new ButtonClickInterface(this), "ButtonClick");
    

    public void setActionSelectListener(ActionSelectListener actionSelectListener) 
        this.mActionSelectListener = actionSelectListener;
    

    public void setVerseToolbarClickListener(ButtonClickListener mButtonClick) 
        this.mButtonClick = mButtonClick;
    

    public void dismissAction() 
        releaseAction();
    


    private class ActionSelectInterface 

        CustomActionWebView mContext;

        ActionSelectInterface(CustomActionWebView c) 
            mContext = c;
        

        @JavascriptInterface
        public void callback(final String value, final int menuID, final String parentDivID) 
            if (mActionSelectListener != null) 
                mActionSelectListener.onTextSelectionMenuClick(menuID, value, parentDivID);
            
        

        @JavascriptInterface
        public void hl_click(long row_id) 
            if (mActionSelectListener != null) 
                mActionSelectListener.onHighlightClick(row_id);
            
        
    

    private class ButtonClickInterface 

        CustomActionWebView mContext;

        ButtonClickInterface(CustomActionWebView c) 
            mContext = c;
        

        @JavascriptInterface
        public void call(final String id) 
            if (mButtonClick != null) 
                mButtonClick.onClick(id);
            
        
    


    private class CustomWebViewClient extends WebViewClient 

        private boolean mLastLoadFailed = false;

        @Override
        public void onPageFinished(WebView webView, String url) 
            super.onPageFinished(webView, url);
            if (!mLastLoadFailed) 
                CustomActionWebView customActionWebView = (CustomActionWebView) webView;
                customActionWebView.linkJSInterface();

            
        

        @Override
        public void onPageStarted(WebView webView, String url, Bitmap favicon) 
            super.onPageStarted(webView, url, favicon);
        

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) 
            super.onReceivedError(view, request, error);
            mLastLoadFailed = true;
        
    



但是,此实现会导致标准 web 视图“复制”和“全选”菜单项的缺失。我可以实现我自己的 COPY 菜单项,但是我不知道如何拥有等效于标准的“SELECT ALL”菜单项,或者以这样的方式实现我自己的自定义 webview 菜单,以便它保留标准菜单项。 谁能建议我如何让“全选”菜单项工作?

【问题讨论】:

【参考方案1】:

我绝对不鼓励您在需要使用旧菜单中的功能时创建新的选择菜单。

关键: 你只需要修改当前的

是的,我们可以通过添加新的选择标签或删除旧的选择标签来编辑当前的选择菜单。

代码说明:

@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) 
    // Get the original action menu
    ActionMode modifiedMode = super.startActionMode(callback, type);

    // Obtain the menu reference
    Menu menu = modifiedMode.getMenu();

    // add new selection with click listener
    menu.add("test").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() 
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) 
            Toast.makeText(getContext(),"مرحباً", Toast.LENGTH_LONG).show();
            return true;
        
    );

    // remove the 'cut' selection from menu
    menu.removeItem(android.R.id.cut);

    return modifiedMode;

【讨论】:

这个答案没有说明如何在 webview 中实现“全选”命令 @Ivan 你的问题标题是:How do I keep standard 'select all' and 'copy' menu items in the WebView text selection menu? 不是如何实现它。所以我建议编辑你的问题标题。 但您的回答也没有说明如何保留标准全选命令 @Ivan 如果您尝试了我的回答,您会发现它按预期工作,您可以在系统选择菜单上进行修改,添加新的选择选项卡或删除一个。 我们可以通过document.execCommand('selectAll')document.execCommand('copy')实现全选或复制

以上是关于如何在 WebView 文本选择菜单中保留标准的“全选”和“复制”菜单项?的主要内容,如果未能解决你的问题,请参考以下文章

如何覆盖 webview 的文本选择上下文菜单?

android webview:防止文本选择actionMode actionBar

如何在 Android 的 web 视图中禁用文本选择?

如何从可以在新 PDF 中选择文本的 WebView 导出 PDF

在Android的WebView中选择文本时如何覆盖上下文操作栏?

Android:如何从 webview 中选择文本