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

标签 android text line android-edittext punctuation

默认情况下,如果行长于 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/

相关文章:

android - 在 build.gradle 中指定 Maven 依赖项不起作用

jquery 将文本标签转为 html 标签

PostgreSQL - 通过 ts_stat 将词素转换为完整的单词

objective-c - 我如何在 Xcode 中为这条线创建橡皮擦?

batch-file - 如何使用批处理文件将文本文件的第一行写入新的文本文件?

algorithm - 如何删除由线段和颜色 block 分隔的区域?

android - 我无法让 genymotion 运行我的应用程序

android - 在使用 Google 登录后,在应用程序中获取生日

android - 如何在 Xamarin.Android 中使用 "values-v11"文件夹

c# - 重写文本时不保留非英文字符