默认情况下,如果行长于 View ,Android EditText 将换行,如下所示:
Thisisalineanditisveryverylongs (end of view)
othisisanotherline
或者如果该行包含标点符号,如下所示:
Thisisalineanditsnotsolong; (several characters from the end of view)
butthisisanotherline
作为我工作的要求,只有当行长于 View 时,文本才必须换行,如下所示:
Thisisalineanditsnotsolong;andt (end of view)
hisisanotherline
一定有办法实现这一目标,对吗?到目前为止,我还没有找到这样做的办法。
最佳答案
TextView(和 EditText)中断文本的方式是通过内部对 BoringLayout 的私有(private)函数调用。因此,最好的方法是继承 EditText 并重写这些函数。但这不是一项微不足道的任务。
所以,在 TextView class有不同类别的文本样式的创建。我们看的是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 - 如何防止EditText在标点符号后换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6134457/