我有一个WebView,我想当有人点击它应该在不在我的应用程序中的浏览器中打开的链接时。我怎么能这样做?
我想展示第一个网页但是当有人点击它时,它应该在新的浏览器中打开,这就是我想要的。我怎么能这样做?
以下是我正在使用的代码
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
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;
}
}
答案 0 :(得分:0)
只需删除webView.setWebViewClient(new WebViewClient());
或添加此
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
答案 1 :(得分:0)
1)在您的活动中添加一个课程。
piecemap = zip(range(MAPWIDTH), range(MAPHEIGHT))
2)private class AppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
新创建的类。您已经拥有一个,您应该删除并替换为以下内容。
setWebViewClient
答案 2 :(得分:0)
From,
当用户点击WebView中网页的链接时,默认行为是Android启动处理网址的应用程序。通常,默认Web浏览器会打开并加载目标网址。
默认行为是,当您在WebView中单击链接时,android将使用Intent.ACTION_VIEW启动它。通过向WebView提供WebViewClient来更改默认行为
只需删除您提供给WebView的WebViewClient,您的应用就可以使用默认流程。
答案 3 :(得分:0)
代码中的注释告诉您这不会在新链接中打开代码。
// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());
您需要覆盖方法shouldOverrideUrlLoading。
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
返回值为true表示主机应用程序处理URL,而返回false表示当前WebView处理URL