Java “如何打开html字符串”;a href";到网络视图?
如何仅在webview上打开包含Java “如何打开html字符串”;a href";到网络视图?,java,android,webview,Java,Android,Webview,如何仅在webview上打开包含a href的html字符串,而不是在浏览器上打开 我使用的是LinkMovementMethod,在其他应用程序(如youtube视频)上打开它没问题,但我有一些链接只需要在webview上打开 下面是一个示例html字符串 String html_text = "<h1>sample text</h1><p><small>February 1 1970</small></p><p
a href
的html字符串,而不是在浏览器上打开
我使用的是LinkMovementMethod,在其他应用程序(如youtube视频)上打开它没问题,但我有一些链接只需要在webview上打开
下面是一个示例html字符串
String html_text = "<h1>sample text</h1><p><small>February 1 1970</small></p><p class=\"text-center\"><img src=\"https://www.google.com\" /></p><p>sample (<a href=\"https://google.com\">here</a>).</p>"
非常感谢您的帮助。//创建一个未编码的HTML字符串
// Create an unencoded HTML string
// then convert the unencoded HTML string into bytes, encode
// it with Base64, and load the data.
String html_text =
"<h1>sample text</h1><p><small>February 1 1970</small></p><p class=\"text-center\"><img src=\"https://www.google.com\" /></p><p>sample (<a href=\"https://google.com\">here</a>).</p>";
String encodedHtml = Base64.encodeToString(html_text.getBytes(),
Base64.NO_PADDING);
newsWebView.loadData(encodedHtml, "text/html", "base64");
//然后将未编码的HTML字符串转换为字节,编码
//使用Base64进行加载,并加载数据。
字符串html_文本=
“示例文本1970年2月1日示例()。”;
字符串encodedHtml=Base64.encodeToString(html_text.getBytes(),
Base64.无填充);
loadData(encodedHtml,“text/html”,“base64”);
我希望这将对您有所帮助。您的问题说明您需要在Web视图中打开单击的url,而不是导航到浏览器。我可以想出两种方法
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
Textview
来显示可点击链接,那么您可以将字符串加载到WebView
本身(如其他人所述),并设置WebView客户端
(这一部分很重要)WebView
。如果没有客户端,它将导航到浏览器。但您应该注意,这将加载并替换其中的内容,所以您的原始链接将不会出现在屏幕上
TextView
单击,只需设置newcontenttv.setMovementMethod(LinkMovementMethod.getInstance())
将不工作,因为默认情况下,它会产生正常的意图,导致它导航到浏览器或相关应用程序,您需要删除它。然后,您必须创建一个扩展LinkMovementMethod
的自定义类,并覆盖touch事件以将单击的url加载到您的webview中,如下所示:文本视图上使用此选项,如
newsContentTv.setMovementMethod(CustomLinkMovementMethod.getInstance());
看看这是否适合你,没想到答案会这么长。Hi@amit它还是一样的,只是将内容加载到html表单中。但a href链接仍将进入浏览器。请尝试此操作。请不要仅发布代码作为答案,还要提供代码的作用以及如何解决问题的解释。带有解释的答案通常质量更高,更容易吸引选票。
newsWebView.loadDataWithBaseURL(null, html_text, "text/html", "UTF-8", null);
newsWebView.setWebViewClient(new WebViewClient());
// add below line to support youtube videos etc.
newsWebView.getSettings().setJavaScriptEnabled(true);
newsWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static CustomLinkMovementMethod instance;
public static CustomLinkMovementMethod getInstance() {
if (instance == null)
instance = new CustomLinkMovementMethod();
return instance;
}
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX() - widget.getTotalPaddingLeft() + widget.getScrollX();
int y = (int) event.getY() - widget.getTotalPaddingTop() + widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] urlSpans = buffer.getSpans(off, off, URLSpan.class);
if (urlSpans.length != 0) {
// You can check log to see if you are getting the url on click.
Log.d("TAG", String.valueOf(urlSpans[0].getURL()));
/* If you've made this an inner class in your activity
you can directly load the url into your webview.
Else you will have to create listeners to get url in activity
on event trigger */
newsWebView.loadData(urlSpans[0].getURL());
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
}
newsContentTv.setMovementMethod(CustomLinkMovementMethod.getInstance());