Android Tutorials TextView parsing HTML samples with pictures _android

Source: Internet
Author: User

Copy Code code as follows:

public class Mainactivity extends activity {

Private Handler Handler;
Private String html;
Private TextView TV;
Private ProgressBar Bar;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
HTML data found on the web
html = "<html><head><title>textview using html</title></head><body><p>< strong> emphasis </strong></p><p><em> Italic </em></p> "
+ "<p><a href=\" http://www.jb51.net > Hyperlink html Getting Started </a> learning Html!</p><p><font color=\ "# Aabb00\ "> Color 1"
+ "</p><p><font color=\" #00bbaa \ "> Color 2</p><h1> title 1</h1><h3> title 2</h3 ><h6> title 3</h6><p> greater than > less than <</p><p> "
+ "Below is the network picture </p><img src=\" http://www.jb51.net/1207.jpg\ "/></body>"
+ "Below is the network picture </p><img src=\" http://www.jb51.net/207.jpg\ "/></body></html>";

TV = (TextView) This.findviewbyid (r.id.id);
Bar = (ProgressBar) This.findviewbyid (R.id.id_bar);
Tv.setmovementmethod (Scrollingmovementmethod.getinstance ());//scrolling

Handler = new Handler () {
@Override
public void Handlemessage (msg) {
TODO auto-generated Method Stub
if (msg.what = = 0x101) {
Bar.setvisibility (View.gone);
Tv.settext ((charsequence) msg.obj);
}
Super.handlemessage (msg);
}
};

Because downloading pictures from the Internet is a time-consuming operation, opening a new thread
Thread t = new Thread (new Runnable () {
Message msg = Message.obtain ();

@Override
public void Run () {
TODO auto-generated Method Stub
Bar.setvisibility (view.visible);
/**
* To achieve the display of the picture you need to use a refactoring method of html.fromhtml: public static spanned
* Fromhtml (String source, Html.imagegetterimagegetter,
* Html.taghandler
* Taghandler) where Html.imagegetter is an interface, we want to implement this interface, in its getdrawable
* (String Source) method to return the Drawable object of a picture.
*/
Imagegetter imagegetter = new Imagegetter () {

@Override
Public drawable getdrawable (String source) {
TODO auto-generated Method Stub
URL url;
Drawable drawable = null;
try {
url = new URL (source);
drawable = Drawable.createfromstream (
Url.openstream (), NULL);
Drawable.setbounds (0, 0,
Drawable.getintrinsicwidth (),
Drawable.getintrinsicheight ());
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return drawable;
}
};
Charsequence test = html.fromhtml (Html, imagegetter, NULL);
Msg.what = 0x101;
Msg.obj = test;
Handler.sendmessage (msg);
}
});
T.start ();
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}

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.

Tags Index: