“word-wrap: break-word” in EditText

佐手、 提交于 2019-12-01 03:38:26

问题


Has an android a css-like property "word-wrap"?

I just want to my text is not wrapped by spaces, dashes, etc., something like this:

  1. hello, w
  2. orld

Instead of

  1. hello,
  2. world

回答1:


Unfortunately, android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.

class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;

    private WordBreakTransformationMethod() {}

    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }

        return instance;
    }

    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};

    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};

    @Override
    protected char[] getOriginal()
    {
        return original;
    }

    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}

'\u2011' is non-breaking dash, '\u00A0' is non-breaking space. Unfortunately, UTF hasn't non-breaking analog for slash ('/'), but you can use division slash (' ∕ ').

For use this code, set instance of WordBreakTransformationMethod to your EditText.

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());


来源:https://stackoverflow.com/questions/22289161/word-wrap-break-word-in-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!