我有一个编辑文本,它有多个彩色文本,如下面的代码.有没有办法存储文本和颜色并再次显示它们?我曾经使用 SharedPreference 来存储字符串整数等,但这不适用于 Spannables.
I got a edittext and it has multiple coloured text like that code below. Is there any way to store text and colours and show them again? I used to use SharedPreference to store strings integers etc. but this isn`t working for Spannables.
Spannable span1 = new SpannableString("this");
Spannable span2 = new SpannableString("is");
Spannable span3 = new SpannableString("text");
span1.setSpan(new ForegroundColorSpan(Color.parseColor("#292929")), 0, span1.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span2.setSpan(new ForegroundColorSpan(Color.parseColor("#2980b9")), 0, span2.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span3.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, span3.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edittext.setText(TextUtils.concat(span1, span2, span3));
你可以使用 Html.toHtml()
得到 Spannable
的 HTML 表示,然后转换它使用 Html.fromHtml()
返回.但是,您需要对此进行彻底测试,因为并非所有格式都能在往返过程中保留下来.
You can use Html.toHtml()
to get an HTML representation of the Spannable
, then convert it back using Html.fromHtml()
. However, you will want to test this thoroughly, as not every bit of formatting survives the round-trip.
这篇关于Android Spanable 文本保存/存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!