我想在点击或标签链接时打开webview上的每个链接到Chrome自定义标签。
final WebView webView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(uri);
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith(uri)){
webView.loadUrl(uri);
}else{
Uri uri = Uri.parse(url);
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.build().launchUrl(MainActivity.this, uri);
}
}
});
答案 0 :(得分:1)
您必须在外部网络浏览器中重定向您的网址,例如
final WebView webView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("http")) { // Could be cleverer and use a regex
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
mContext.startActivity(intent);
return true;
}
return false;
}
@Override
public void onLoadResource(WebView view, String url) {
}
});