使用 loadDataWithBaseURL 禁用 webview 中的链接
Using loadDataWithBaseURL disables links in webview
我使用以下代码加载 html 电子书内容,其中 templateString 包含连接到主文件中的样式表和图像的 html 内容。
String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", "about:blank");
我面临的问题是锚链接根本没有响应。
我注意到如果 itemURL 为 null 或者如果我使用 loadData 而不是 loadDataWithBaseURL,链接有效但我松开了通过 itemURL 连接的图像和样式的连接。
请注意,网络视图可见性始终设置为可见。
添加 我在 webview 中添加了以下功能
this.getSettings().setJavaScriptEnabled(true);
this.requestFocusFromTouch();
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
this.getSettings().setDisplayZoomControls(false);
this.getSettings().setAllowContentAccess(true);
this.getSettings().setAllowFileAccess(true);
this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.getSettings().setAllowFileAccessFromFileURLs(true);
this.getSettings().setAllowUniversalAccessFromFileURLs(true);
这是为 webview 初始化的 onTouch 方法:
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
System.out.println("getExtra: "+hr.getExtra());
// getExtra always gives null when webview loaded with loadDataWithBaseURL while it should give the url of the link pressed when the user touches a link
return false;
}
});
如果需要更多代码,我可以分享。
Can you try to load HTML text instead of giving URL as below:-
webview.loadDataWithBaseURL(null, htmltext, "text/html", "utf-8", null);
将 WebViewClient
设置为您的 webView
并在 loadDataWithBaseURL
中加载数据并传递您的基础 url
这将有助于将锚 url 加载到 webview
webview.getSettings().setJavaScriptEnabled(true);
webview.requestFocusFromTouch();
webview.setWebViewClient(new MyWebClient());
这里是WebViewClient
class
class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
}
}
问题似乎与 baseURL 无关,而是与连接到 WebView 的 css 文件有关。在没有 baseURL 的情况下,css 文件未加载,这使得脚注可以点击。
css 文件中导致问题的代码行是:
pointer-events: none;
因此,如果有人正在寻找一种使 webview 非交互的方法,这就是一种方法。
感谢为这个问题做出贡献的每一个人。
我使用以下代码加载 html 电子书内容,其中 templateString 包含连接到主文件中的样式表和图像的 html 内容。
String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL, templateString, "text/html", "UTF-8", "about:blank");
我面临的问题是锚链接根本没有响应。
我注意到如果 itemURL 为 null 或者如果我使用 loadData 而不是 loadDataWithBaseURL,链接有效但我松开了通过 itemURL 连接的图像和样式的连接。
请注意,网络视图可见性始终设置为可见。 添加 我在 webview 中添加了以下功能
this.getSettings().setJavaScriptEnabled(true);
this.requestFocusFromTouch();
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
this.getSettings().setDisplayZoomControls(false);
this.getSettings().setAllowContentAccess(true);
this.getSettings().setAllowFileAccess(true);
this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.getSettings().setAllowFileAccessFromFileURLs(true);
this.getSettings().setAllowUniversalAccessFromFileURLs(true);
这是为 webview 初始化的 onTouch 方法:
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
System.out.println("getExtra: "+hr.getExtra());
// getExtra always gives null when webview loaded with loadDataWithBaseURL while it should give the url of the link pressed when the user touches a link
return false;
}
});
如果需要更多代码,我可以分享。
Can you try to load HTML text instead of giving URL as below:-
webview.loadDataWithBaseURL(null, htmltext, "text/html", "utf-8", null);
将 WebViewClient
设置为您的 webView
并在 loadDataWithBaseURL
中加载数据并传递您的基础 url
这将有助于将锚 url 加载到 webview
webview.getSettings().setJavaScriptEnabled(true);
webview.requestFocusFromTouch();
webview.setWebViewClient(new MyWebClient());
这里是WebViewClient
class
class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
}
}
问题似乎与 baseURL 无关,而是与连接到 WebView 的 css 文件有关。在没有 baseURL 的情况下,css 文件未加载,这使得脚注可以点击。
css 文件中导致问题的代码行是:
pointer-events: none;
因此,如果有人正在寻找一种使 webview 非交互的方法,这就是一种方法。
感谢为这个问题做出贡献的每一个人。