Android Spannable text save/store issue(Android Spanable 文本保存/存储问题)
问题描述
我有一个编辑文本,它有多个彩色文本,如下面的代码.有没有办法存储文本和颜色并再次显示它们?我曾经使用 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 文本保存/存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android Spanable 文本保存/存储问题
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
