webView加载html两种方式

2017-10-12  本文已影响11人  Jannonx

加载main/assets目录下的html文件

![YJ7FRC@@29SGXIEA%$C9Z6.png

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="http://www.google.com" id="cse-search-box">
  <div>
    <p>google search</p>
    <input type="text" name="q" size="20" />
    <input type="submit" name="sa" value="Search" />
  </div>
</form>
</body>
</html>
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.loadUrl("file:///android_asset/bxd-note.html");
  }

加载html格式的字符串

 String html_str="<!DOCTYPE html>"
                + "<html lang='en'>"
                + "<head>"
                + "</head>"
                + "<body>"
                + "<form action='http://www.google.com'  id='cse-search-box'>"
                + "<div>"
                + "<p>google search</p>"
                + "<input type='text' name='q' size='20' />"
                + "<input type='submit' name='sa' value='Search' />"
                + "</div>"
                + "</form>"
                + "</body>"
                + "</html>";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webView);
        String html_str="<!DOCTYPE html>"
                + "<html lang='en'>"
                + "<head>"
                + "</head>"
                + "<body>"
                + "<form action='http://www.google.com'  id='cse-search-box'>"
                + "<div>"
                + "<p>google search</p>"
                + "<input type='text' name='q' size='20' />"
                + "<input type='submit' name='sa' value='Search' />"
                + "</div>"
                + "</form>"
                + "</body>"
                + "</html>";

        mWebView.loadDataWithBaseURL(null, html_str, "text/html", "utf-8", null);
    }
上一篇 下一篇

猜你喜欢

热点阅读