问题描述
我有一个WebView,我想当有人单击某个链接时,应该在浏览器中而不是在我的应用程序中打开它.我该怎么办?
I have a WebView and I wanted to when someone click on a link it should open in a browser not in my app. How can I do that?
我想显示第一个网页,但是当有人单击它时,它应该在我想要的新浏览器中打开.我该怎么办?
I want to show a first webpage but when someone click on it it should open in a new browser that's all I want. How can i do that?
这是我正在使用的代码
package com.packagename.weebly.free_recharge_app;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2, container, false);
WebView webView = (WebView) view.findViewById(R.id.webview1);
webView.getSettings().setSupportZoom(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
// String appCachePath = this.getCacheDir().getAbsolutePath();
// Log.e(TAG, "appCachePath = " + appCachePath);
// webView.getSettings().setAppCachePath(appCachePath);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://google.com");
return view;
}
}
添加代码@eugene
after adding your code @eugene
package com.freerechargeapp.weebly.free_recharge_app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2, container, false);
WebView webView = (WebView) view.findViewById(R.id.webview1);
webView.getSettings().setSupportZoom(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
// String appCachePath = this.getCacheDir().getAbsolutePath();
// Log.e(TAG, "appCachePath = " + appCachePath);
// webView.getSettings().setAppCachePath(appCachePath);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl("google.com");
return view;
}
}
推荐答案
代码中的注释告诉您,这将不会在新链接中打开代码.
The Comment in your code is telling you that this will not open the code in a new link.
// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());
您需要重写方法shouldOverrideUrlLoading.
You Need to Override the method shouldOverrideUrlLoading.
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
返回值true表示宿主应用程序处理URL,而返回false表示当前WebView处理URL
A return value of true means the host application handles the URL, while return false means the current WebView handles the URL
这篇关于链接正在webview中打开,我想在android studio中的默认浏览器中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!