编辑文本视图中的某些文本

时间:2019-06-20 07:18:00

标签: java android

我正在寻找一种在文本视图上获得“编辑过的”效果的方法

redacted document

关于我应该如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:1)

来自Markus Kauppinen的上述评论。

String body = "Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard...";

List<String> textsToRedact = new ArrayList<>();

textsToRedact.add("paper");
textsToRedact.add("rock");
textsToRedact.add("lizard poisons Spock");

textView.setLetterSpacing(-0.1f); //reducing letter spacing to get a solid black line
textView.setText(redact(body, textsToRedact));

这是一个hack,还有很多改进的余地。

private String redact(String body, List<String> texts) {

    body = body.replaceAll(".(?!$)", "$0 "); //Giving an extra space in between body characters.

    for (String text : texts) {
        text = text.replaceAll(".(?!$)", "$0 "); //Giving extra space in between characters to match the length.
        int length = text.length();

        if (length > 0) {
            String replacement = String.format("%0" + length + "d", 0).replace("0", "▉");
            body = body.replaceAll(text, replacement); //or use replaceFirst (according to your need)
        }
     }

    return body;
}

screenshot