android教程之textview解析带图片的html示例

2019-12-10 20:10:39王旭
本文介绍的示例适用于android中需要解析带图片的htlm数据,需要的朋友可以参考下    

 

复制代码
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数据
  html = "<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"
    + "<p><a href="http://www.easck.com/a>学习HTML!</p><p><font color="#aabb00">颜色1"
    + "</p><p><font color="#00bbaa">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>"
    + "下面是网络图片</p><img src="http://www.easck.com/1207.jpg"/></body>"
    + "下面是网络图片</p><img src="http://www.easck.com/207.jpg"/></body></html>";

  tv = (TextView) this.findViewById(R.id.id);
  bar = (ProgressBar) this.findViewById(R.id.id_bar);
  tv.setMovementMethod(ScrollingMovementMethod.getInstance());// 滚动

  handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    if (msg.what == 0x101) {
     bar.setVisibility(View.GONE);
     tv.setText((CharSequence) msg.obj);
    }
    super.handleMessage(msg);
   }
  };

  // 因为从网上下载图片是耗时操作 所以要开启新线程
  Thread t = new Thread(new Runnable() {
   Message msg = Message.obtain();

   @Override
   public void run() {