我正在尝试在 Android 中使用 Webview 打开链接。 WebView 中嵌入了一些链接。我的问题是 webview 无法打开任何不以 www 开头的链接。例如,www.google.com 可以工作,但maps.google.com 不能。我也尝试覆盖 WebViewClient,但没有成功。 我注意到的一件事是通过放置 Toast 来查看 WebViewClient 中正在调用什么 url。它对 www.google.com 显示完美,但对其他链接没有返回任何内容。我认为在这种情况下 WebViewClient 不会被覆盖。可能是什么原因。我是否必须调用 webview.js 的任何方法或某些属性? 任何帮助将不胜感激。
menuView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
Toast.makeText(getApplicationContext(), "url:--" + url, Toast.LENGTH_LONG).show();
view.loadUrl(url);
return false;
}
});
我已经设置的属性是:
menuView.setVerticalScrollBarEnabled(false);
menuView.setHorizontalScrollBarEnabled(false);
final WebSettings webSettings = menuView.getSettings();
menuView.getSettings().setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
menuView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
menuView.getSettings().setBuiltInZoomControls(true);
// Below required for geolocation
menuView.getSettings().setJavaScriptEnabled(true);
menuView.getSettings().setGeolocationEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
最佳答案
// in oncreate
webview.setWebChromeClient(new wecrome());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.setWebViewClient(new MyBrowser());
webview.getSettings().setPluginState(PluginState.ON);
webview.loadUrl("http://www.example.net/locations/");
//inner class
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object() {
@JavascriptInterface
public void performClick() throws Exception {
Log.d("LOGIN::", "Clicked");
Toast.makeText(googleplus.this, "Login clicked",
Toast.LENGTH_LONG).show();
}
}, "login");
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
System.out.println("started");
pd.show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
pd.dismiss();
System.out.println("ends");
super.onPageFinished(view, url);
}
}
关于android - WebView 链接打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266177/