问题描述
我正在Android中创建自己的文本处理器(一个自定义垂直脚本TextView for Mongolian)。我以为我必须自己找到所有换行位置才能实现换行,但后来发现。这似乎找到了各种语言中的字符,单词,行和句子之间的所有可能的中断。
I'm making my own text processor in Android (a custom vertical script TextView for Mongolian). I thought I would have to find all the line breaking locations myself so that I could implement line wrapping, but then I discovered BreakIterator
. This seems to find all the possible breaks between characters, words, lines, and sentences in various languages.
我正在尝试学习如何使用它。 比平均更有帮助,但仍然很难从阅读中理解。我还找到了一些教程(参见,,)但他们缺乏完整的解释我正在寻找的输出。
I'm trying to learn how to use it. The documentation was more helpful than average, but it was still difficult to understand from just reading. I also found a few tutorials (see here, here, and here) but they lacked the full explanation with output that I was looking for.
我正在添加此Q& A样式答案,以帮助我自己学习如何使用 BreakIterator
。
I am adding this Q&A style answer to help myself learn how to use BreakIterator
.
除了Java之外,我还把它变成了Android标签,因为有和未来的答案可以解决这个问题。
I'm making this an Android tag in addition to Java because there is apparently some difference between them. Also, Android now supports the ICU BreakIterator
and future answers may deal with this.
推荐答案
BreakIterator
可用于查找字符,单词,行和句子之间可能的中断。这对于将光标移动到可见字符,双击以选择单词,三击以选择句子和换行等内容非常有用。
BreakIterator
can be used to find the possible breaks between characters, words, lines, and sentences. This is useful for things like moving the cursor through visible characters, double clicking to select words, triple clicking to select sentences, and line wrapping.
以下示例中使用了以下代码。只需调整第一部分即可更改 BreakIterator
的文本和类型。
The following code is used in the examples below. Just adjust the first part to change the text and type of BreakIterator
.
// change these two lines for the following examples
String text = "This is some text.";
BreakIterator boundary = BreakIterator.getCharacterInstance();
// boiler plate code
boundary.setText(text);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; end = boundary.next()) {
System.out.println(start + " " + text.substring(start, end));
start = end;
}
如果您只想测试一下,可以将其直接粘贴到Android中的Activity onCreate
。我正在使用 System.out.println
而不是 Log
,以便它也可以在仅Java环境中测试。
If you just want to test this out, you can paste it directly into an Activity's onCreate
in Android. I'm using System.out.println
rather than Log
so that it is also testable in a Java only environment.
我正在使用 java.text.BreakIterator
而不是ICU,只能从API获得24.有关详细信息,请参阅底部的链接。
I'm using the java.text.BreakIterator
rather than the ICU one, which is only available from API 24. See the links at the bottom for more information.
更改样板代码以包含关注
Change the boilerplate code to include the following
String text = "Hi 中文éé\uD83D\uDE00\uD83C\uDDEE\uD83C\uDDF3.";
BreakIterator breakIterator = BreakIterator.getCharacterInstance();
输出
这篇关于BreakIterator如何在Android中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!