如何仅在输入 2 个字符后才在 GWT SuggestBox 中显示建议?
How to show suggestions in a GWT SuggestBox only after 2 characters have been typed?
我正在尝试制作一个仅在输入 2 个字符后才显示建议的 SuggestBox。我的想法是在文本长度为 1 时隐藏建议,使用 class DefaultSuggestionDisplay。我尝试在 SuggestionBox 本身及其 TextBox 上附加不同的处理程序,如 KeyPressHandler 和 KeyUpHandler,但其中 none 似乎有效。你有 "suggestions" 吗? :D
您可以扩展 SuggestBox 并覆盖 showSuggestionList()
方法。
添加 KeyUpHandler 不起作用,因为您添加了另一个 KeyUpHandler,而不是替换 SuggestBox 添加到它自己的 TextBox 的那个。
编辑:
@Override
showSuggestionList() {
if (getTextBox().getValue().length() > 1) {
super.showSuggestionList();
}
}
您可以扩展 DefaultSuggestionDisplay
并覆盖 showSuggestions
方法:
public class MySuggestionDisplay extends DefaultSuggestionDisplay {
@Override
protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
if(suggestBox.getText().length() > 1)
super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
}
}
您必须将新显示传递给 SuggestBox
构造函数:
public class MySuggestBox extends SuggestBox {
public MySuggestBox() {
super(
new MySuggestOracle(),
new TextBox(),
new MySuggestionDisplay());
}
}
在此构造函数中,您应该提供:
- 你自己的
SuggestOracle
class(此处命名为 MySuggestOracle
)- 我想你有一个
TextBox
- 它是默认的输入文本的小部件(你可以提供自己的,它只需要实现 HasText
)
SuggestionDisplay
- 使用覆盖了 showSuggestions
方法的方法。
这是完整的工作示例代码,显示了至少输入 2 个字符的建议:
import java.util.ArrayList;
import java.util.Collection;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.TextBox;
public class MySuggestBox extends SuggestBox {
public MySuggestBox() {
super(
new SuggestOracle() {
@Override
public void requestSuggestions(Request request, Callback callback) {
ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
suggestions.add(new MySuggestion("aaa"));
suggestions.add(new MySuggestion("bbb"));
suggestions.add(new MySuggestion("ccc"));
suggestions.add(new MySuggestion("ddd"));
Response response = new Response();
response.setSuggestions(suggestions);
callback.onSuggestionsReady(request, response);
}
},
new TextBox(),
new MySuggestionDisplay());
}
public static class MySuggestionDisplay extends DefaultSuggestionDisplay {
@Override
protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
if(suggestBox.getText().length() > 1)
super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
}
}
public static class MySuggestion implements Suggestion {
private String text;
public MySuggestion(String text) {
this.text = text;
}
@Override
public String getDisplayString() {
return text;
}
@Override
public String getReplacementString() {
return text;
}
}
}
我正在尝试制作一个仅在输入 2 个字符后才显示建议的 SuggestBox。我的想法是在文本长度为 1 时隐藏建议,使用 class DefaultSuggestionDisplay。我尝试在 SuggestionBox 本身及其 TextBox 上附加不同的处理程序,如 KeyPressHandler 和 KeyUpHandler,但其中 none 似乎有效。你有 "suggestions" 吗? :D
您可以扩展 SuggestBox 并覆盖 showSuggestionList()
方法。
添加 KeyUpHandler 不起作用,因为您添加了另一个 KeyUpHandler,而不是替换 SuggestBox 添加到它自己的 TextBox 的那个。
编辑:
@Override
showSuggestionList() {
if (getTextBox().getValue().length() > 1) {
super.showSuggestionList();
}
}
您可以扩展 DefaultSuggestionDisplay
并覆盖 showSuggestions
方法:
public class MySuggestionDisplay extends DefaultSuggestionDisplay {
@Override
protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
if(suggestBox.getText().length() > 1)
super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
}
}
您必须将新显示传递给 SuggestBox
构造函数:
public class MySuggestBox extends SuggestBox {
public MySuggestBox() {
super(
new MySuggestOracle(),
new TextBox(),
new MySuggestionDisplay());
}
}
在此构造函数中,您应该提供:
- 你自己的
SuggestOracle
class(此处命名为MySuggestOracle
)- 我想你有一个 TextBox
- 它是默认的输入文本的小部件(你可以提供自己的,它只需要实现HasText
)SuggestionDisplay
- 使用覆盖了showSuggestions
方法的方法。
这是完整的工作示例代码,显示了至少输入 2 个字符的建议:
import java.util.ArrayList;
import java.util.Collection;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.TextBox;
public class MySuggestBox extends SuggestBox {
public MySuggestBox() {
super(
new SuggestOracle() {
@Override
public void requestSuggestions(Request request, Callback callback) {
ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
suggestions.add(new MySuggestion("aaa"));
suggestions.add(new MySuggestion("bbb"));
suggestions.add(new MySuggestion("ccc"));
suggestions.add(new MySuggestion("ddd"));
Response response = new Response();
response.setSuggestions(suggestions);
callback.onSuggestionsReady(request, response);
}
},
new TextBox(),
new MySuggestionDisplay());
}
public static class MySuggestionDisplay extends DefaultSuggestionDisplay {
@Override
protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
if(suggestBox.getText().length() > 1)
super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
}
}
public static class MySuggestion implements Suggestion {
private String text;
public MySuggestion(String text) {
this.text = text;
}
@Override
public String getDisplayString() {
return text;
}
@Override
public String getReplacementString() {
return text;
}
}
}