使用嵌入式HTML代码加载WebView - Android

时间:2017-04-29 07:51:15

标签: javascript android webview android-webview loaddata

下面我附上了要用webview执行的代码。

  

注意:应该启用JAVASCRIPT

     

任何人都帮助我在WEBVIEW中运行这个HTML字符串

<!DOCTYPE html>
<html>
<body>
<!-- Markup for HTML (Factors in Placement and Enrollment of Primary 
Care Patientsin YMCA&#39;s Diabetes Prevention Program, Bronx, New 
York,2010-2015) --><div class='rid_08184eef_309335' 
data-apiroot='//tools.cdc.gov/api' data-mediatype='html' 
data-mediaid='309335' data-stripscripts='true' data-stripanchors='false' 
data-stripimages='false' data-stripcomments='true' 
data-stripstyles='true' data-cssclasses='syndicate' data-ids='' 
data-xpath='' data-oe='utf-8' data-of='xhtml' data-ns='cdc' 
data-postprocess='' data-nw='true' data-iframe='true' 
data-cdc-widget='syndicationIframe' 
data-apiembedsrc='skins/larry//tools.cdc.gov/api/embed/html/js/embed-2.0.3.js' 
data-iframeembedsrc='skins/larry//tools.cdc.gov/TemplatePackage/contrib/widgets/tp-widget-external-loader.js'></div><script 
src='skins/larry//tools.cdc.gov/TemplatePackage/contrib/widgets/tp-widget-external-loader.js' 
 ></script><noscript>You need javascript enabled to view this content or go to <a href='skins/larry//tools.cdc.gov/api/v2/resources/media/309335/noscript'>source URL</a>.</noscript>

</body>
</html>

我按照link进行了基本设置。什么都没有帮助

以下我附上了我的JAVA代码

public class WebtestActivity extends Activity {
WebView webtest;
final Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.webviewtesting);
    webtest=(WebView)findViewById(R.id.webtest);
    String htmlString = "<!-- Markup for HTML (How to Prevent Cancer or Find It Early) --><div class=\"rid_ec9fb40c_123238\" data-apiroot=\"//tools.cdc.gov/api\" data-mediatype=\"HTML\" data-mediaid=\"123238\" data-stripscripts=\"false\" data-stripanchors=\"false\" data-stripimages=\"false\" data-stripcomments=\"false\" data-stripstyles=\"false\" data-cssclasses=\"syndicate\" data-ids=\"\" data-xpath=\"\" data-oe=\"UTF-8\" data-of=\"XHTML\" data-ns=\"\" data-postprocess=\"\" data-nw=\"true\" data-iframe=\"true\" data-cdc-widget=\"syndicationIframe\" data-apiembedsrc=\"//tools.cdc.gov/api/embed/html/js/embed-2.0.3.js\" data-iframeembedsrc=\"//tools.cdc.gov/TemplatePackage/contrib/widgets/tp-widget-external-loader.js\" data-font=\"\"></div><script src='//tools.cdc.gov/TemplatePackage/contrib/widgets/tp-widget-external-loader.js' ></script><noscript>You need javascript enabled to view this content or go to <a href='//tools.cdc.gov/api/v2/resources/media/123238/noscript'>source URL</a>.</noscript>";
    webtest.getSettings().setJavaScriptEnabled(true);

    webtest.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webtest.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
            Log.d("des===",description);
            Log.d("failingUrl===",failingUrl);

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            Log.d("shouldlLoading===",url);
            view.loadUrl(url);
            return true;
        }
    });

    webtest.loadData(htmlString, "text/html", null);

}

} enter image description here

1 个答案:

答案 0 :(得分:2)

试试这段代码。如果您正在学习,请使用官方文档&gt; WebView

String htmlString = "<html><body>Your text.</body></html>";
browser.getSettings().setJavaScriptEnabled(true);
browser.loadData(htmlString, "text/html", null);

您的代码正常运行。我附上了截图

enter image description here