如果有人点击另一个webpage
他或她无法访问webpage
,我希望阻止外部链接的代码的活动性。
public class NewsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
String url = "https://www.example.com";
WebView web = (WebView) findViewById(R.id.webView4);
web.loadUrl(url);
final WebView mWebView = (WebView) findViewById(R.id.webView4);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setSavePassword(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.getSettings().setLightTouchEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('header_wrapper')[0].style.display='none'; " +
"document.getElementsByClassName('footer-contact')[0].style.display='none'; " +
"document.getElementsByClassName('navbar-header')[0].style.display='none'; " +
"document.getElementsByClassName('footer-social')[0].style.display='none'; " +
"document.getElementById('footer_bottom').style.display='none'; " +
"document.getElementById('footer_content').style.display='none'; " +
"document.getElementById('core_mobile_menu').style.display='none'; " +
"document.getElementById('catapult-cookie-bar').style.display='none'; " +
"})()");
}
});
mWebView.loadUrl("https://www.example.com");
}
}
答案 0 :(得分:1)
根据您的代码尝试此操作
public class NewsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
String url = "https://www.example.com";
WebView web = (WebView) findViewById(R.id.webView4);
web.loadUrl(url);
final WebView mWebView = (WebView) findViewById(R.id.webView4);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setSavePassword(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.getSettings().setLightTouchEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
return true;
}
});
mWebView.loadUrl("https://www.example.com");
}
}
答案 1 :(得分:0)
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url){
//True if the host application wants to leave the current WebView and
// handle the url itself, otherwise return false.
return true;
}
});
像这样使用:
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
Log.d("WebView", "your current url when webpage loading.." + url);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("WebView", "your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});
答案 2 :(得分:0)
尝试此代码,这也将处理Nougat及以上的shouldOverrideUrlLoading
弃用
mWebview.setWebViewClient(new WebViewClent() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// reject anything other by returning true
return true;
}
@RequiresApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url=request.getUrl().toString();
if (Uri.parse(url).getHost().equals("http://www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// reject anything other
return true;
}
}