简体   繁体   English

如何在Android中将一种跨度类型更改为另一种?

[英]How to change one span type to another in Android?

I would like to take all the spans of one type in a CharSequence and convert them to a different type. 我想在CharSequence采用一种类型的所有跨度,并将它们转换为另一种类型。 For example, convert all the bold spans to underline spans: 例如,将所有粗体跨度转换为下划线跨度:

在此处输入图片说明

How would I do that? 我该怎么做?

(This was a problem I was facing today, and since I have solved it now, I am adding a Q&A pair here. My answer is below.) (这是我今天面临的一个问题,因为我现在已经解决了这个问题,所以我在这里添加了一个问答环节。我的答案如下。)

How to change spans from one type to another 如何将跨度从一种类型更改为另一种类型

在此处输入图片说明

In order change the spans, you need to do the following things 为了更改跨度,您需要执行以下操作

  1. Get all the spans of the desired type by using getSpans() 使用getSpans()获取所需类型的所有范围
  2. Find the range of each span with getSpanStart() and getSpanEnd() 使用getSpanStart()getSpanEnd()查找每个范围的范围
  3. Remove the original spans with removeSpan() 使用removeSpan()删除原始范围
  4. Add the new span type with setSpan() in the same locations as the old spans 使用setSpan()在与旧跨度相同的位置添加新跨度类型

Here is the code to do that: 这是执行此操作的代码:

Spanned boldString = Html.fromHtml("Some <b>text</b> with <b>spans</b> in it.");

// make a spannable copy so that we can change the spans (Spanned is immutable)
SpannableString spannableString = new SpannableString(boldString);

// get all the spans of type StyleSpan since bold is StyleSpan(Typeface.BOLD)
StyleSpan[] boldSpans = spannableString.getSpans(0, spannableString.length(), StyleSpan.class);

// loop through each bold span one at a time
for (StyleSpan boldSpan : boldSpans) {

    // get the span range
    int start = spannableString.getSpanStart(boldSpan);
    int end = spannableString.getSpanEnd(boldSpan);

    // remove the bold span
    spannableString.removeSpan(boldSpan);

    // add an underline span in the same place
    UnderlineSpan underlineSpan = new UnderlineSpan();
    spannableString.setSpan(underlineSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Notes 笔记

  • If you want to just clear all the old spans, then use boldString.toString() when creating the SpannableString . 如果您只想清除所有旧的跨度,则在创建SpannableString时使用boldString.toString() You would use the original boldString to get the span ranges. 您将使用原始的boldString来获取跨度范围。

See also 也可以看看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 map function 将列表从一种类型更改为另一种类型 - How to change a list from one type to another with map function 如何在 Android Studio 中更改方向时更改网格布局中的列跨度? - How to change the column span in grid layout on orientation change in Android Studio? 如何在 android 中将焦点从一个编辑框更改为另一个 - How to change focus from one editbox to another in android 如何通过单击Android中的另一个微调器来更改一个微调器上的数据? - How to change data on one spinner by onclick on another spinner in android? 如何实现从一个到另一个 android 的一周变化 - How to implement a change of week from one to another android Android 如何使用行和列跨度实现这种类型的布局 - Android how to achieve this type of layout with rows and col span Android EditText:如何在键入时应用前景颜色跨度? - Android EditText : How to apply Foreground color span as you type? 如何删除 android 中的 GridLayout 跨度计数更改 animation? - How to remove GridLayout span count change animation in android? 在android中,如何计算将位置从一个位置更改为另一个位置所需的时间 - In android, how to calculate the time which takes for a location change from one location to another 如何在Android中将Tabhost片段一个更改为另一个时清除autocompleteTextView字符串 - How to clear the autocompleteTextView string when change the tabhost fragment one to another in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM