代码之家  ›  专栏  ›  技术社区  ›  Sarthak Gandhi

在Edittext中粘贴Html文本

  •  0
  • Sarthak Gandhi  · 技术社区  · 6 年前

    如何在将网站中的某些文本粘贴到EditText时限制HTML内容?.

    例如,我从一个网站上复制了一些文本,当我在EditText中粘贴文本时,HTML字体和文本大小也随之出现。我想要的是在粘贴文本时限制任何HTML内容。尝试了使用TextWatcher和InputFilters的几种方法,但没有一种有效。Whatsapp正在处理这个问题。请帮忙。

    编辑:字符串可以转换为非HTML,但如何在EditText中进行更改?

    1 回复  |  直到 6 年前
        1
  •  0
  •   gi097 Sildoreth    6 年前

    这很可能是 支援多文本格式编写 问题。

    根据这个 question 您可以添加 TextWatcher 给你的 EditText 使用这种方法:

    public void afterTextChanged(Editable string)
    {
        CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(),
                                                    MetricAffectingSpan.class);
        for (int index = 0; index < toBeRemovedSpans; index++)
            string.removeSpan(toBeRemovedSpans[index]);
        }
    }
    

    参考:

    https://stackoverflow.com/a/24777290/5457878