简体   繁体   English

Android WebChrome客户端在浏览器而非WebView中提示/打开链接

[英]Android WebChrome Client Promts/Opens Link in browser rather than WebView

Upon launch of the activity, the webview should load the designated url but in the simulator it launches the native browser and on the physical device it prompts to open the url in in the browser. 在启动活动时,Web视图应加载指定的URL,但在模拟器中它将启动本机浏览器,并在物理设备上提示在浏览器中打开URL。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    WebView wv = findViewById(R.id.my_webview);
    WebSettings webSettings = wv.getSettings();
    wv.setWebChromeClient(new WebChromeClient());
    webSettings.setJavaScriptEnabled(true);
    wv.loadUrl("http://google.com");
}

Trying to get it so that the webview neither launches in native browser or prompts the user to open in browser. 尝试获取它,以使Web视图既不能在本机浏览器中启动,也不会提示用户在浏览器中打开。 Also all embedded links should stay in the webview if clicked. 如果单击,所有嵌入的链接也应保留在Web视图中。

I think you have to implement the shouldOverrideUrlLoading() method: 我认为您必须实现shouldOverrideUrlLoading()方法:

shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return false;
}

This happens if you don't add a WebViewClient to your WebView instance. 如果不将WebViewClient添加到WebView实例,则会发生这种情况。 In order to enable navigation in the same WebView, you need to set a WebViewClient to your WebView instance wv. 为了在同一WebView中启用导航,您需要将WebViewClient设置为您的WebView实例wv。 Add the following line: 添加以下行:

 wv.setWebViewClient(new WebViewClient());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM