如何防止EditText在标点符号后换行

问题描述

| 认情况下,如果该行长于视图,则Android EditText将中断该行,如下所示:
Thisisalineanditisveryverylongs (end of view)
othisisanotherline
或者该行包含标点符号,例如:
Thisisalineanditsnotsolong;     (several characters from the end of view)
butthisisanotherline
作为我工作的要求,仅当行长于视图时,文本才必须换行,如下所示:
Thisisalineanditsnotsolong;andt (end of view)
hisisanotherline
一定有办法做到这一点,对吗?到目前为止,我还没有找到执行此操作的方法。     

解决方法

TextView(和EditText)中断文本的方式是通过内部对BoringLayout的私有函数调用。因此,最好的方法是订阅EditText并重写这些函数。但这将不是一件容易的事。 因此,在TextView类中,为文本样式创建了不同的类。我们看的是DynamicLayout。在此类中,我们到达类StaticLayout的引用(在一个名为reflowed的变量中)。在此类的构造函数中,您将找到文本换行算法:
/*
* From the Unicode Line Breaking Algorithm:
* (at least approximately)
*  
* .,:; are class IS: breakpoints
*      except when adjacent to digits
* /    is class SY: a breakpoint
*      except when followed by a digit.
* -    is class HY: a breakpoint
*      except when followed by a digit.
*
* Ideographs are class ID: breakpoints when adjacent,* except for NS (non-starters),which can be broken
* after but not before.
*/

if (c == \' \' || c == \'\\t\' ||
((c == \'.\'  || c == \',\' || c == \':\' || c == \';\') &&
(j - 1 < here || !Character.isDigit(chs[j - 1 - start])) &&
(j + 1 >= next || !Character.isDigit(chs[j + 1 - start]))) ||
((c == \'/\' || c == \'-\') &&
(j + 1 >= next || !Character.isDigit(chs[j + 1 - start]))) ||
(c >= FIRST_CJK && isIdeographic(c,true) &&
j + 1 < next && isIdeographic(chs[j + 1 - start],false))) {
okwidth = w;
ok = j + 1;
这就是所有包装的去处。因此,您需要子类化处理StaticLayout,DynamicLayout,TextView以及最后的EditText-我确定-这将是一场噩梦:(我什至不知道所有流程如何。如果需要的话-请先看看TextView并检查是否有getLinesCount调用-这将是起点。     ,Android中的这种换行算法确实很烂,在逻辑上甚至都不正确-逗号不能成为一行的最后一个字符。它只会产生不必要的换行符,从而导致极其奇怪的文本布局。     ,嗨,这是我首先从另一个人那里得到的一种方法,然后进行一些更改,对我来说真的很有效,您可以尝试一下。
//half ASCII transfer to full ASCII
public static String ToSBC(String input) { 
    char[] c = input.toCharArray(); 
    for (int i = 0; i< c.length; i++) { 
    if (c[i] == 32) { 
    c[i] = (char) 12288; 
    continue; 
    } 
    if (c[i]<=47 && c[i]>32 ) 
    c[i] = (char) (c[i] + 65248); 
    } 
    return new String(c); 
    }
}
这里是。我将某些特殊字符从半角更改为全角,例如\“,\” \“。\”,效果很好。你可以试试看。