我正在爲禁用peaople的Displaykeyboard工作,我正在考慮添加自動字填充功能。 我找到了一個來自oracle的例子,可以在我需要的時候工作。它的另一個例子:TextAreaDemo。問題是我真的不明白搜索算法,問題是當我添加一些單詞到arraylist搜索算法停止正常工作。自動完成功能Java
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;
}
有沒有辦法修改這個例子,以便它可以處理任何單詞?
下面的鏈接,例如http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html – QFireball
'binarySearch'依靠排序輸入,所以我猜你不會在添加單詞後再次對列表進行排序? –
該列表將是靜態的,所以這不是真的needet – QFireball