次のコードを使用して、すべての太字のテキストを配列内の文字列として保存するスパン文字列をふるいにかけます。
StyleSpan[] spans = storyText.getSpans(0,
storyText.length(), StyleSpan.class);
List<String> boldedWords = new ArrayList<String>();
for (StyleSpan span : spans) {
if (span.getStyle() == Typeface.BOLD) {
//start
int s = storyText
.getSpanStart(span);
//end
int e = storyText.getSpanEnd(span);
boldedWords.add(storyText.subSequence(
s, e).toString());
}
}
String[] array = boldedWords
.toArray(new String[boldedWords.size()]);
ただし、配列に返された文字列は順不同です。例えば:
文は次のようになります (CAPS は太字を表します):
storyText = "This ADJECTIVE NOUN is VERB"
返される配列は、「名詞、動詞、形容詞」の順です。「形容詞、名詞、動詞」
なぜこれが起こっているのかについての洞察はありますか?