如果tv.setText(Html.fromHtml(text));
花费太长时间,并挂起用户界面,我该怎么办?
如果我能用线程来做,你能提供一个例子吗?
答案 0 :(得分:6)
private Handler mHandler = new Handler() {
void handleMessage(Message msg) {
switch(msg.what) {
case UPDATE_TEXT_VIEW:
tv.setText(msg.obj); // set text with Message data
break;
}
}
}
Thread t = new Thread(new Runnable() {
// use handler to send message to run on UI thread.
mHandler.sendMessage(mHandler.obtainMessage(UPDATE_TEXT_VIEW, Html.fromHtml(text));
});
t.start();
答案 1 :(得分:3)
如果您不需要解析长HTML或复杂的HTML,则Spannable
的手动编写比使用Html.fromHtml()
要快得多。以下示例来自Set color of TextView span in Android
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);