Kyl*_*yle 105 android android-webview android-browser
现在我有一个加载webview的应用程序,所有点击都保存在应用程序中.我想要做的是当某个链接,例如,在应用程序中单击http://www.google.com时,它会打开默认浏览器.如果有人有想法请告诉我!
Amo*_*tir 182
今天我必须做同样的事情,我在StackOverflow上找到了一个非常有用的答案,我想在这里分享以防其他人需要它.
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Cri*_*vez 32
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl(https://whatoplay.com/);
Run Code Online (Sandbox Code Playgroud)
你不必包含这段代码
// webview.setWebViewClient(new WebViewClient());
Run Code Online (Sandbox Code Playgroud)
相反,你需要使用下面的代码
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url2="https://whatoplay.com/";
// all links with in ur site will be open inside the webview
//links that start ur domain example(http://www.example.com/)
if (url != null && url.startsWith(url2)){
return false;
}
// all links that points outside the site will be open in a normal android browser
else
{
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Piy*_*ush 11
你可以使用Intent:
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);
Run Code Online (Sandbox Code Playgroud)
小智 10
您只需添加以下行
yourWebViewName.setWebViewClient(new WebViewClient());
Run Code Online (Sandbox Code Playgroud)
请查看此官方文档.
小智 6
您可以使用Intent:
Uri uriUrl = Uri.parse("http://www.google.com/");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
97051 次 |
最近记录: |