本文介紹了Android Spanable 文本保存/存儲問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個編輯文本,它有多個彩色文本,如下面的代碼.有沒有辦法存儲文本和顏色并再次顯示它們?我曾經使用 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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!