简体   繁体   English

自动完成功能Java

[英]Auto completion function Java

I am working on a Displaykeyboard for disabled peaople and i am thinking about to adding a auto word completion function. 我正在为残疾人使用peaople的Displaykeyboard,并且我正在考虑添加自动单词补全功能。 I found a example from oracle that works as i need it. 我从oracle找到了一个可以按需工作的示例。 Its the Another Example: TextAreaDemo. 其另一个示例:TextAreaDemo。 The problem is i dont really understand the search algorithm and the problem is when i add some word to the arraylist the search algorithm stops working properly. 问题是我不太了解搜索算法,问题是当我在数组列表中添加一些单词时,搜索算法会停止正常工作。

String prefix = content.substring(w + 1).toLowerCase();
int n = Collections.binarySearch(words, prefix);
if (n < 0 && -n <= words.size()) {
    String match = words.get(-n - 1);
    if (match.startsWith(prefix)) {
        // A completion is found
        String completion = match.substring(pos - w);
        // We cannot modify Document from within notification,
        // so we submit a task that does the change later
        SwingUtilities.invokeLater(
        new CompletionTask(completion, pos + 1));
}
} else {
// Nothing found
mode = Mode.INSERT;
}

Is there a way to modify the example so it will work with any words? 有没有办法修改示例,使其可以使用任何单词?

Make sure you're not just adding the word to the end of the list and then using binarySearch() . 确保您不仅将单词添加到列表的末尾,然后使用binarySearch() It's documentation says the following 它的文档说以下

The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method) prior to making this call. 在进行此调用之前,必须根据列表元素的自然顺序将其按升序排序(例如通过sort(List)方法)。

Read more about it here: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T) 在此处了解更多信息: https : //docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM