本文介绍: 修改webviewpackage com.whsz.mywebtest;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.JavascriptInter.
package com.whsz.mywebtest;
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 java.util.ArrayList;
import java.util.List;
/**
* Created by guoshuyu on 2017/6/16.
*/
public class CustomActionWebView extends WebView {
static String TAG = "CustomActionWebView";
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返回给原生接口
*IE9以下支持:document.selection
* IE9、Firefox、Safari、Chrome和Opera支持:window.getSelection()
*/
private void getSelectedData(String title) {
String js = "(function getSelectedText() {" +
"var txt1 =window.getSelection().getRangeAt(0).startContainer.data;" +
"var txt2 =window.getSelection().toString();" +
"var title = "" + title + "";" +
"JSInterface.callback(txt1,txt2,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 parentText, final String value, final String title) {
if(mActionSelectListener != null) {
mActionSelectListener.onClick(title, value,parentText);
}
}
}
}
package com.whsz.mywebtest;
public interface ActionSelectListener {
void onClick(String title,String parentText, String selectText);
}
package com.whsz.mywebtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
CustomActionWebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.webview);
List<String> list = new ArrayList<>();
// list.add("复制");
// list.add("分享");
list.add("批注");
//设置item
mWebView.setActionList(list);
//链接js注入接口,使能选中返回数据
mWebView.linkJSInterface();
//增加点击回调
mWebView.setActionSelectListener((title, selectText,parent) -> {
String info = "内容: " + selectText+ "。n段落: " + parent;
Toast.makeText(this, info, Toast.LENGTH_LONG).show();
});
// 开启javascript 渲染
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);//关键点
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setDisplayZoomControls(false);
webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
webSettings.setAllowFileAccess(true); // 允许访问文件
webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
webSettings.setSupportZoom(true); // 支持缩放
webSettings.setLoadWithOverviewMode(true);
mWebView.setWebViewClient(new WebViewClient());
// 载入内容
// mWebView.loadUrl("file:///android_asset/111.html");
mWebView.loadData("qweweeeeeeeeeeqewq","text/html","utf-8");
}
}
原文地址:https://blog.csdn.net/qq_28934205/article/details/124198008
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_10603.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。