去除HTML标签(初级)
处理方式:使用Spanned处理
步骤:1.数据转换成Spanned
2.Spanned不调用tostring 显示原本样式
3.Spanned调用tostring 不显示原本样式
java逻辑代码
// 去除html标签
private final String mHtml = "<b>asdfjl</b> <a>www.baidu.com</a>";
private final String mString = "asdfjl www.baidu.com";
private TextView mHtmlView;
private TextView mStringView;
private TextView mHtmlTostringView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spanned sp = Html.fromHtml(mHtml);
mHtmlView = (TextView) findViewById(R.id.html);// 显示html
mHtmlView.setText(sp);
mStringView = (TextView) findViewById(R.id.string);// 显示普通string
mStringView.setText(mHtml);
mHtmlTostringView = (TextView) findViewById(R.id.htmltostring);//显示取出样式的html
mHtmlTostringView.setText(sp.toString());;
}
}
效果图: