Android会为代码

时间:2017-03-06 08:39:01

标签: android

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

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);

1 个答案:

答案 0 :(得分:1)

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));

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