机器人的WebView - 如何保持用户在应用程序中,当他们点击的WebView链接? [英] Android WebView - how to keep the user inside the app when they click on WebView links?

查看:117
本文介绍了机器人的WebView - 如何保持用户在应用程序中,当他们点击的WebView链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个命令,以保持用户仍然在应用程序中他们点击一个web视图中的链接后。但我失去了它,似乎无法找到它是什么。

任何人都可以请提醒我说,命令是什么? 谢谢!

这是我的code:

  @覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);

    的WebView的WebView =新的WebView(本);
    webview.getSettings()setAppCacheEnabled(假)。
    webview.getSettings()setJavaScriptEnabled(真)。
    webview.setInitialScale(1);
    。webview.getSettings()setPluginState(PluginState.ON);

    webview.setWebViewClient(新WebViewClient(){
        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            view.loadUrl(URL);
            返回true;
        }
    });

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(真正的);
    webSettings.setBuiltInZoomControls(真正的);
    webSettings.setAllowContentAccess(真正的);
    webSettings.setEnableSmoothTransition(真正的);
    webSettings.setLoadsImagesAutomatically(真正的);
    webSettings.setLoadWithOverviewMode(真正的);
    webSettings.setSupportZoom(真正的);
    webSettings.setUseWideViewPort(真正的);

    的setContentView(web视图);
    webview.loadUrl("http://www.stitcher.com/podcast/entre$p$pneuronfirecom/entre$p$pneur-on-fire-tim-ferriss-other-incredible-entre$p$pneurs");
 

解决方案

要捕获所有的点击web视图中,您需要设置一个自定义WebViewClient与 shouldOverrideUrlLoading 设置为

  mWebView.setWebViewClient(新WebViewClient(){
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        view.loadUrl(URL);
        返回true;
    }
});
 

如果没有这样做,将在浏览器中打开。

<一个href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29">http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String中)

  

给主机应用程序有机会接管控制,当一个新的URL是关于当前的WebView加载。如果未提供WebViewClient,默认的WebView会要求活动管理器来选择适当的处理程序的URL。

I had this command to keep the user still inside the app after they clicked links inside a webview. But I lost it and cannot seem to find what it is.

Could anyone please remind me what that command is? Thank you!

This is my code:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    WebView webview = new WebView(this);
    webview.getSettings().setAppCacheEnabled(false);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setInitialScale(1);
    webview.getSettings().setPluginState(PluginState.ON);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setAllowContentAccess(true);
    webSettings.setEnableSmoothTransition(true);
    webSettings.setLoadsImagesAutomatically(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setSupportZoom(true);
    webSettings.setUseWideViewPort(true);

    setContentView(webview);
    webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");          

解决方案

To capture all clicks within the webView, you need to set a custom WebViewClient with shouldOverrideUrlLoading set to true

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

Without doing this, it will open in the browser.

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url.

这篇关于机器人的WebView - 如何保持用户在应用程序中,当他们点击的WebView链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆