不要导航到WebView中的其他页面,禁用链接和引用 [英] Don't navigate to other pages in WebView,disable links and references
问题描述
我在Android中有一个webView,然后在其中打开一个html网页.但是它充满了链接和图像,当我单击其中的一个时,它会加载到我的Web视图中.我想禁用此行为,因此,如果我单击链接,请不要加载它.我已经尝试过此解决方案并进行了一些编辑为了自己,但没有成功. 我的webview客户端代码:
I have a webView in Android, and I open a html webpage in it. But it's full of links and images, and when I click one of them, it loads in my webview. I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked. My webviewclient code:
private boolean loaded = false;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(loaded == false){
view.loadUrl(url);
loaded = true;
return true;
}else{
return false;
}
}
我的webview实施和设置.
My webview implementation and settings.
WebView wv = (WebView) findViewById(R.id.recipeWv);
ourWebViewClient webViewClient = new ourWebViewClient();
webViewClient.shouldOverrideUrlLoading(wv, URLsave);
wv.setWebViewClient(webViewClient);
wv.setFocusableInTouchMode(false);
wv.setFocusable(false);
wv.setClickable(false);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
示例:如果用户在我的Web视图中打开StackOverflow主页并单击其中一个链接(例如问题"),则该Web视图应停留在StackOverflow主页上.
Example: If the user open in my webview the StackOverflow homepage and clicks on one of the links(like "Questions") then the webview should stay on the StackOverflow homepage.
推荐答案
您应将当前的URL保存在类变量中,并检查其是否与已加载的URL相同.当用户单击链接时,将调用shouldOverrideUrlLoading
并检查网站.
像这样:
You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading
is called and check the website.
Something like this:
private String currentUrl;
public ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
重要:不要忘记将WebViewClient
设置为您的WebView
.
Important: don't forget set the WebViewClient
to your WebView
.
ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);
这篇关于不要导航到WebView中的其他页面,禁用链接和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!