gpt4 book ai didi

Android 为标签之间的所有文本着色

转载 作者:行者123 更新时间:2023-11-29 01:11:36 25 4
gpt4 key购买 nike

你假设我有这个用一些标签格式化的字符串:

String content = "11111<mft:A>22222</mft:A>111111<mft:R>33333<mft:A>22222</mft:A>333333<mft:S>444444</mft:S></mft:R><mft:A>2222222</mft:A>";

我想像这个屏幕截图一样通过提取标签来为这个字符串着色:

Android colorize all texts between tags

不幸的是,我编写的代码不正确,并且无法在标签之间正确着色:

我的结果代码:

enter image description here

下面是我的代码,它不正确地为标签之间的文本着色

private List<AyehaTagsInformation>   ayeHaPositions     = new ArrayList<>();
private List<AsharTagsInformation> sherHaPositions = new ArrayList<>();
private List<RevayatTagsInformation> revayatHaPositions = new ArrayList<>();

private void extractTags(String str) {
Pattern mftA_REGEX = Pattern.compile("<mft:A>(.+?)</mft:A>");
Pattern mftR_REGEX = Pattern.compile("<mft:R>(.+?)</mft:R>");
Pattern mftS_REGEX = Pattern.compile("<mft:S>(.+?)</mft:S>");

Matcher matcher = mftA_REGEX.matcher(str);
while (matcher.find()) {
ayeHaPositions.add(new AyehaTagsInformation(matcher.start(1), matcher.end(1)));
}

matcher = mftR_REGEX.matcher(str);
while (matcher.find()) {
revayatHaPositions.add(new RevayatTagsInformation(matcher.start(1), matcher.end(1)));
}

matcher = mftS_REGEX.matcher(str);
while (matcher.find()) {
sherHaPositions.add(new AsharTagsInformation(matcher.start(1), matcher.end(1)));
}
}


String content = "11111<mft:A>22222</mft:A>111111<mft:R>33333<mft:A>22222</mft:A>333333<mft:S>444444</mft:S></mft:R><mft:A>2222222</mft:A>";
extractTags(content);

Spannable wordToSpan = new SpannableStringBuilder(content);

if (revayatHaPositions.size() > 0) {
for (int p = 0; p < revayatHaPositions.size(); p++) {
try {
wordToSpan.setSpan(new ForegroundColorSpan(Color.GREEN), revayatHaPositions.get(p).getStart(), revayatHaPositions.get(p).getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error colorize: ", e.getMessage());
}
}
}

if (ayeHaPositions.size() > 0) {
for (int p = 0; p < ayeHaPositions.size(); p++) {
try {
wordToSpan.setSpan(new ForegroundColorSpan(Color.BLUE), ayeHaPositions.get(p).getStart(), ayeHaPositions.get(p).getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error colorize: ", e.getMessage());
}
}
}

if (sherHaPositions.size() > 0) {
for (int p = 0; p < sherHaPositions.size(); p++) {
try {
wordToSpan.setSpan(new ForegroundColorSpan(Color.RED), sherHaPositions.get(p).getStart(), sherHaPositions.get(p).getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error colorize: ", e.getMessage());
}
}
}

message.setText(wordToSpan);

最佳答案

String s1= "11111<mft:A>";
String s2= "<font color='#ff0000'>22222</font>";
String s3= "</mft:A><font color='#0000ff'>111111</font><mft:R>";
t.setText(Html.fromHtml(s1+ s2+s3));

使用相同格式的正则表达式来概括答案

关于Android 为标签之间的所有文本着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com