我需要使用自定义命令扩展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; } } }
但是,此实现导致缺少标准的Webview“ COPY”和“ SELECT ALL”菜单项。我可以实现自己的COPY菜单项,但是我不知道如何等效于标准的“全选”菜单项或如何实现自己的自定义Webview菜单,以使其保留标准的菜单项。谁能建议我如何使“全选”菜单项起作用?