第一步,将下面的两个类复制到项目中:
1.接口类:
/** * author: wu * date: on 2018/5/30. * describe:webView复制接口 */ public interface ActionSelectListener { void onClick(String title, String selectText); }
2.工具类:
import android.content.Context; import android.os.Build; import android.util.AttributeSet; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.webkit.JavascriptInterface; import android.webkit.WebView; import com.miaomi.miaomiao.interfaces.ActionSelectListener; import java.util.ArrayList; import java.util.List; /** * author: wu * date: on 2018/5/30. * describe:webView复制文字 */ public class CustomActionWebView extends WebView { ActionMode mActionMode; List<String> mActionList = new ArrayList<>(); ActionSelectListener mActionSelectListener; 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); } /** * 处理item,处理点击 * @param actionMode */ private ActionMode resolveActionMode(ActionMode actionMode) { if (actionMode != null) { final Menu menu = actionMode.getMenu(); mActionMode = actionMode; menu.clear(); for (int i = 0; i < mActionList.size(); i++) { menu.add(mActionList.get(i)); } for (int i = 0; i < menu.size(); i++) { MenuItem menuItem = menu.getItem(i); menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { getSelectedData((String) item.getTitle()); 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; } } /** * 点击的时候,获取网页中选择的文本,回掉到原生中的js接口 * @param title 传入点击的item文本,一起通过js返回给原生接口 */ private void getSelectedData(String title) { String js = "(function getSelectedText() {" + "var txt;" + "var title = \"" + title + "\";" + "if (window.getSelection) {" + "txt = window.getSelection().toString();" + "} else if (window.document.getSelection) {" + "txt = window.document.getSelection().toString();" + "} else if (window.document.selection) {" + "txt = window.document.selection.createRange().text;" + "}" + "JSInterface.callback(txt,title);" + "})()"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { evaluateJavascript("javascript:" + js, null); } else { loadUrl("javascript:" + js); } } public void linkJSInterface() { addJavascriptInterface(new ActionSelectInterface(this), "JSInterface"); } /** * 设置弹出action列表 * @param actionList */ public void setActionList(List<String> actionList) { mActionList = actionList; } /** * 设置点击回掉 * @param actionSelectListener */ public void setActionSelectListener(ActionSelectListener actionSelectListener) { this.mActionSelectListener = actionSelectListener; } /** * 隐藏消失Action */ public void dismissAction() { releaseAction(); } /** * js选中的回掉接口 */ private class ActionSelectInterface { CustomActionWebView mContext; ActionSelectInterface(CustomActionWebView c) { mContext = c; } @JavascriptInterface public void callback(final String value, final String title) { if(mActionSelectListener != null) { mActionSelectListener.onClick(title, value); } } } }
第二步:xml 文件中应用:
<com.miaomi.miaomiao.utils.home.CustomActionWebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" />
最后一步:在代码中添加:
List<String> list = new ArrayList<>(); list.add("复制"); webView.setWebViewClient(new CustomWebViewClient()); //设置item webView.setActionList(list); //链接js注入接口,使能选中返回数据 webView.linkJSInterface(); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setDisplayZoomControls(false); //使用javascript webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setDomStorageEnabled(true); //增加点击回调 webView.setActionSelectListener(new ActionSelectListener() { @Override public void onClick(String title, String selectText) { ToastUtil.show(mContext, "复制成功"); // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager,虽然提示deprecated,但不影响使用。 ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); // 将文本内容放到系统剪贴板里。 cm.setText(selectText); } }); webView.loadUrl(url);
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; } }
@Override protected void onPause() { super.onPause(); if(webView != null) { webView.dismissAction(); } }