我需要从webview中获取所选文本。
为此,我说了这个:
webView.loadUrl("javascript:Android.getHtml(window.getSelection().toString())");
在我的触摸事件中。
Android.getHtml
效果很好(这是javascript接口方法)window.getSelection
没有任何价值:它的问题 - >在我看来,当我在webview中触摸一个单词时,window.getSelection会自动包含文本。我错了吗?有没有人能够对此有所了解?
非常感谢提前。
Ps:下面是我的整个代码
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_each_news, container, false);
//설정된 인자값을 받는다
String newsAddress = getArguments().getString("newsAddress");
//Start : 받은 주소로 웹뷰를 띄운다...
webView = (WebView) rootView.findViewById(R.id.webView);
// webView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new WebViewClient() {});
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); //자바스크립트 허용여부
webSettings.setLoadWithOverviewMode(true); //웹페이지 웹뷰크기에 맞추기 위한 설정
webSettings.setUseWideViewPort(true); //웹페이지 웹뷰크기에 맞추기 위한 설정
webView.setInitialScale(1); //웹페이지 웹뷰크기에 맞추기 위한 설정
webView.addJavascriptInterface(new MyJavascriptInterface(), "Android"); //자바스크립트 인터페이스 메소드 등록
//url 호출
webView.loadUrl(newsAddress);
webView.setOnTouchListener(this);
//End : 받은 주소로 웹뷰를 띄운다...
return rootView;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
// Toast.makeText(getContext(),"웹뷰클릭",Toast.LENGTH_LONG).show();
pressStartTime = System.currentTimeMillis();
pressedX = motionEvent.getX();
pressedY = motionEvent.getY();
stayedWithinClickDistance = true;
break;
}
case MotionEvent.ACTION_MOVE: {
if (stayedWithinClickDistance && getDistance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
break;
}
case MotionEvent.ACTION_UP: {
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
// 클릭처리
webView.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>()
{
@Override
public void onReceiveValue(String value)
{
Log.v(TAG, "SelectedText:" + value);
}
});
}
}
}
return false;
}
答案 0 :(得分:0)
对于API&gt; = 19,您可以使用evaluateJavascript
mWebview.evaluateJavascript("(function(){return window.getSelection().toString()})()",
new ValueCallback<String>()
{
@Override
public void onReceiveValue(String value)
{
Log.v(TAG, "Webview selected text: " + value);
}
});
对于API&lt; 19您可以使用evaluateJavascript
使用loadUrl
方法,如下所示:
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.addJavascriptInterface(new JavaScriptInterface(), "javascriptinterface");
然后,当您想要进行选择时,请使用以下内容:
mWebview.loadUrl("javascript:javascriptinterface.callback(window.getSelection().toString())");
并定义一个WebAppInterface类,如下所示:
public class JavaScriptInterface
{
@JavascriptInterface
public void callback(String value)
{
Log.v(TAG, "SELECTION:" + value);
}
}