简体   繁体   English

Android WebView href不支持HTML页面

[英]Android webview href not support in html page

I want to load to a about.html page inside asset folder by clicking a button in index.html inside android webview. 我想通过单击android webview中index.html中的按钮来加载到资产文件夹中的about.html页面。

Here's the code: 这是代码:

Button: 按钮:

<a href="about.html">About</a>

Java: Java的:

    WebView browser = (WebView) findViewById(webview);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.setWebChromeClient(new WebChromeClient());
    browser.loadUrl("file:///android_asset/www/index.html");
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

But the problem is when i click on button it doesn't load about.html but if I add a javascript function with below code it works.. but i want to use href. 但是问题是,当我单击按钮时,它不会加载about.html,但是如果我添加具有以下代码的javascript函数,它将起作用..但我想使用href。

Code: 码:

<button onclick="about()">About</button>
 <script>
        function about() {
            location.href = "about.html";
        }   
</script>

How do I solve it? 我该如何解决?

write an interface for TagHandler and in your fromHtml method check for the tag and handle it by loading the page 为TagHandler写一个接口,并在fromHtml方法中检查标签并通过加载页面来处理它

https://developer.android.com/reference/android/text/Html.html https://developer.android.com/reference/android/text/Html.html

try this code: 试试这个代码:

private void loadWebView(WebView webView,String htmlString)
{
 try {
         final String mimeType = "text/html";
         final String encoding = "UTF-8";

            WebViewClient yourWebClient = new WebViewClient()
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView  view, String  url)
                {
                 // This line we let me load only pages inside  Webpage
                 if ( url.contains("") == true )
                    // Load new URL Don't override URL Link
                    return false;

                 // Return true to override url loading (In this case do nothing).
                 return true;
                }
            };


            // Get Web view
            webView.getSettings().setJavaScriptEnabled(true);   
            webView.getSettings().setSupportZoom(true); 
            webView.getSettings().setBuiltInZoomControls(true);
            webView.setWebViewClient(yourWebClient);

            // Load URL
            webView.loadDataWithBaseURL("", htmlString, mimeType, encoding, "");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

use it as: 用作:

loadWebView(browser, "file:///android_asset/www/index.html")  //pass url 

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

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