GWT KeyPress过滤器

时间:2011-06-06 20:29:31

标签: java gwt filter keypress

如何创建KeyPress过滤器,该过滤器仅强制数字作为文本字段中的输入。这里有类似的东西:
http://www.smartclient.com/smartgwt/showcase/#form_keypress_filter

因为添加了anoyence有没有办法在uiBinder xml文件中执行此操作?

1 个答案:

答案 0 :(得分:6)

执行此操作的代码描述为here

在uiBinder文件中,定义TextBox项目。

<g:TextBox uifield='textBox'/>

在您的课程中,您可以直接添加KeyPressHandler,例如:

textBox.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            if (!"0123456789".contains(String.valueOf(event.getCharCode()))) {
                textBox.cancelKey();
            }
        }

    });

或者您可以像这样使用@UiHandler注释:

@UiHandler("testBox")
public void onKeyPress(KeyPressEvent event) {
    if (!"0123456789".contains(String.valueOf(event.getCharCode()))) {
        textBox.cancelKey();
    }
}
相关问题