问题描述
我有一个加载页面的简单 webview.此页面有一些在 webview 中打开的链接.这就是它应该做的,所以一切正常.
I have a simple webview which loads a page. This page has a few links that opens within the webview. That's what it supposed to do, so it's working all fine.
但是该页面中有一个链接应该作为弹出窗口加载,所以我希望当人们点击它时它在普通浏览器中打开.但正如我所说,所有链接都在 webview 中打开,因此该链接也会打开.
But there is one single link from that page which should load as a popup, so I want it to open in the normal browser when people click it. But as I stated, all links are opening in the webview, so that link does it also.
我的问题是,如何让这个链接在普通浏览器中作为一种弹出窗口打开?甚至有可能吗?链接是可变的,所以它总是在变化,它不能在应用程序中硬编码以在新的浏览器浏览器中打开.
My question is, how can I make this link open in the normal browser as a kind of a popup? Is it even possible? The link is variable so it's changing always, it cannot be hardcoded within the application to open in a new browser browser.
有可能吗,我该怎么做?
Is it possible and how can I do it?
推荐答案
以下是覆盖 webview 加载以留在 webview 中或离开的示例:
Here's an example of overriding webview loading to stay within your webview or to leave:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestWebViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
}
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somePartOfYourUniqueUrl")){ // Could be cleverer and use a regex
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
} else {
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
}
这篇关于在普通浏览器中打开来自 Android Webview 的链接作为弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!