简体   繁体   English

HTML格式化字符串插入TextViews和EditText

[英]HTML Formatted String inserting into TextViews and EditText

All, 所有,

I have a database that will store an HTML tagged text to retain formatting information from an EditText. 我有一个数据库,将存储HTML标记的文本,以保留EditText中的格式信息。 I create this string using HTML.toHtml(EditText.getText). 我使用HTML.toHtml(EditText.getText)创建此字符串。 I notice this method wraps whatever Spanned Text is put in it with <p> and </p> . 我注意到这个方法用<p> and </p>包装任何跨越文本。 The issue with that is when I got to use the method HTML.fromHtml(HTMLFormattedString) and then use the setText method of either a TextView or EditText there are two extra lines at the end of my actual text, which makes sense because that is how the paragraph tag works with HTML. 问题是当我使用方法HTML.fromHtml(HTMLFormattedString)然后使用TextView或EditText的setText方法时,在我的实际文本的末尾有两个额外的行,这是有道理的,因为这是如何段落标记适用于HTML。

My question is is there anyway to make the textView or EditText shrink to not display the extra blank lines? 我的问题是,无论如何要使textView或EditText缩小以不显示额外的空白行? What is the simplest way to do this? 最简单的方法是什么? I have experimented with just removing the last <p> and </p> , but that only works if the user did not enter 3 or more new lines with the return key. 我已经尝试过删除最后一个<p> and </p> ,但这只有在用户没有使用返回键输入3个或更多新行时才有效。

I ended up searching for white space at the end of the spanned text that was created and removed it. 我最终在创建并删除它的跨区文本末尾搜索空格。 This took care of extra spaces due to the <p> </p> and was less time consuming than overriding the mentioned class to achieve the same results. 由于<p> </p> ,这会占用额外的空间,并且比重写上述类以获得相同的结果更省时。

public SpannableStringBuilder trimTrailingWhitespace(
        SpannableStringBuilder spannableString) {

    if (spannableString == null)
        return new SpannableStringBuilder("");

    int i = spannableString.length();

    // loop back to the first non-whitespace character
    while (--i >= 0 && Character.isWhitespace(spannableString.charAt(i))) {
    }

    return new SpannableStringBuilder(spannableString.subSequence(0, i + 1));
}

Well this is just a round about approach. 那么这只是一个圆的方法。 I had the same issue. 我遇到过同样的问题。 And you are provided with two options, 并为您提供两种选择,

1)As you said that paragraph tag works the way what you have suspected. 1)正如你所说,段落标签的工作方式与你所怀疑的一样。 What it does , it appends two "\\n" values to the end of each <\\p> tag. 它做什么,它在每个<\\ p>标签的末尾附加两个“\\ n”值。 So you can convert the html to string and remove the last two characters which are usually two "\\n"s 因此,您可以将html转换为字符串并删除最后两个通常为两个“\\ n”的字符

or 要么

2) You have get into the Html Class itself. 2)你已经进入了Html类本身。 That is, you have to override the HTML class and look for handleP(SpannableStringBuilder text) and change its core logic a little bit. 也就是说,您必须覆盖HTML类并查找handleP(SpannableStringBuilder text)并稍微更改其核心逻辑。

private static void handleP(SpannableStringBuilder text) {
    int len = text.length();

    if (len >= 1 && text.charAt(len - 1) == '\n') {
        if (len >= 2 && text.charAt(len - 2) == '\n') {
            return;
        }
        text.append("\n");
        return;
    }

    if (len != 0) {
        text.append("\n\n");


    }
} 

As you can see here, it appends two "\\n" in len!=0 which is were you have to do the change. 正如你在这里看到的那样,它在len!=0附加两个“\\ n”,你必须进行更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM