Loading HTML code with WebView

Source: Internet
Author: User

When using EditText to display HTML strings, EditText does not parse any HTML tags, but instead displays all the HTML tags directly-----as if they were displayed in a regular Notepad, or if the application wants to re-parse the HTML string, As an HTML page to display, it is also possible.

WebView provides a loaddata (string data, String MimeType, String encoding) method that can be used to load and display HTML code, but during actual use, when it loads content containing Chinese HTML, the Web The view will display garbled characters.

WebView also provides a loaddatawithbaseurl (string baseUrl, String data, String MimeType, String encoding, String history URL) method, which is an enhanced version of the LoadData (string data, String MimeType, String encoding) method, which does not produce garbled characters.

1. Data: Specifies the HTML code that needs to be loaded.

2. MimeType: Specifies the MIME type of HTML code, which can be specified as text/html for HTML code.

3. Encoding: Specifies the character set used for encoding the HTML code. For example, designated as GBK.

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.webkit.WebView;

public class Viewhtml extends Activity {
WebView Show;

@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_view_html);
//Get the WebView component in the program
show = (WebView) Findviewbyid (r.id.show);
StringBuilder sb = new StringBuilder ();
//splicing a piece of HTML code
sb.append ("
sb.append ("
sb.append ("<title> welcome you </title>");
sb.append ("
sb.append ("<body>");
sb.append (" "
+ "Crazy Java Alliance </a>
sb.append ("</body>");
sb.append ("
//Using a simple LoadData method can lead to garbled, possibly an Android API bug
//show.loaddata (sb.tostring (), "text/html", "Utf-8");
//load, and display HTML code
Show.loaddatawithbaseurl (null,sb.tostring (), "text/html", "Utf-8", null);
}
}

Loading HTML code with WebView

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.