“word-wrap: break-word” in EditText

后端 未结 1 1473
予麋鹿
予麋鹿 2021-01-12 05:35

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
相关标签:
1条回答
  • 2021-01-12 05:47

    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());
    
    0 讨论(0)
提交回复
热议问题