Android display HTML tag or with picture

Source: Internet
Author: User

Display HTML files in Android with Html.fromhtml (...) The returned value of the processed return value can be a parameter of SetText ().

Only HTML with text can handle HTML files in the following ways.

 Public Static Spanned fromhtml (String source)  

To display HTML with pictures, use the following method to process the HTML file.

 Public Static Spanned fromhtml (String source, Html.imagegetter Imagegetter, Html.taghandler Taghandler)  

Imagegetter to process the processor in HTML, generate the Drawable object and return it.

Creating Imagegetter primarily implements the following method, source is the value of the SRC attribute in the tag.

 Public Drawable getdrawable (String source)  

The following example displays HTML in TextView and EditView and inserts a picture.

Only HTML text is displayed, and clicking the button adds a picture after the TextView and EditView text.

 Public classAndroidtest2activity extends Activity {/** Called when the activity is first created.*/TextView TV;    EditText et;    Button Addpic;    String CT; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); ET= (EditText) This. Findviewbyid (R.ID.EDITTEXT1); TV= (TextView) This. Findviewbyid (r.id.tv); CT="aaa<font color=\ "red\" >aaa</font>"; Addpic= (Button) This. Findviewbyid (R.id.addpic); Addpic.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubct+=""+r.drawable.icon+"\ "/>";            Refreshview ();              }                    });                    Refreshview (); }    Private voidRefreshview () {Et.settext (html.fromhtml (Ct,imagegetter,NULL)); Tv.settext (html.fromhtml (Ct,imagegetter,NULL)); } imagegetter Imagegetter=NewImagegetter () {@Override Publicdrawable getdrawable (String source) {intID =integer.parseint (source); Drawable D=getresources (). getdrawable (ID); D.setbounds (0,0, D.getintrinsicwidth (), D. Getintrinsicheight ()); returnD;  }      }; 

1. Jump to the browser to access the page directly, this code is copied in the activity, so there is a startactivity () method

Uri  uri = Uri.parse ("http://www.baidu.com"// address to link new  Intent (Intent.action_view, URI); startactivity (Intent);

2. Displaying HTML methods using TextView

TextView Text1 = (TextView) Findviewbyid (r.id.textview02); Text1.settext (html.fromhtml ("<font size="  > Web Content </font> "));

3. Direct use of the display Web Component in Android WebView

WebView = (webview) Findviewbyid (r.id.webview01); Webview.getsettings (). setjavascriptenabled (True  ); Webview.loadurl ("http://www.baidu.com"

Android display HTML tag or with picture

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.