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