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 文本保存/存储问题
基础教程推荐
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
