本文介绍了如何在GWT中为BlurEvent正确添加UiHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果GWT中有一些 textField ,则可以通过以下代码添加BlurHandler:

Normally if we have some textField in GWT we can add a BlurHandler by the following code:

textField.addBlurHandler(new BlurHandler() {
        @Override
        public void onBlur(BlurEvent event) {
             //what we need    
        }
    });

但是,如果我们使用UiBinder并且我们的 textField 由@UiField注释,并且在我们的ui.xml文件中提到,我们也可以通过以下代码添加BlurHandler:

But if we use UiBinder and our textField is annotated by @UiField and it is mentioned in our ui.xml file we can add BlurHandler by this code as well:

@UiHandler("textField")
protected void createBlurHandler(BlurEvent event) {

}

我想我就在这里,因为它像这样工作.因此,问题是, 我们可以在ui.xml文件中实际定义BlurHandler吗?

I guess I am right here because it works like this. So, the question is, can we actually define BlurHandler inside ui.xml file?

例如,可以在其中添加 inputMaxLength 和其他一些属性,GWT是否具有 onChange 方法之类的可能性,或者这些方法是我描述的唯一可能性?

For example, it is possible to add inputMaxLength and some other attributes there, does GWT has some possibility like onChange method or are these ways that I described the only possibilities?

我想要这样的东西:

<g:TextBox ui:field="textField" onBlur="methodName" />

有可能吗?

推荐答案

我很确定您要问的问题是不可能的.问题在于您将无法使用反射来确定要调用的方法.但是,您可以扩展TextBox类,并在模板中使用它.扩展类可以具有可以在模板中设置的自己的属性.下面是一个示例,其中我在自己的DefaultTextBox上设置了默认测试.

I am pretty sure what you are asking is not possible. The problem is that you wouldn't be able to use reflection to figure out which method you want to call. However you can extends the TextBox class and use that inside your template. The extended class could have it's own properties that can be set in the template. An example is as follows where I set the default test on my own DefaultTextBox.

public class DefaultTextBox extends TextBox {

    /**
     * The text color used when the box is disabled and empty.
     */
    private static final String TEXTBOX_DISABLED_COLOR = "#AAAAAA";

    private final String defaultText;

    public @UiConstructor
    DefaultTextBox(final String defaultText) {
        this.defaultText = defaultText;
        resetDefaultText();

        // Add focus and blur handlers.
        addFocusHandler(new FocusHandler() {
            @Override
            public void onFocus(FocusEvent event) {
                getElement().getStyle().clearColor();
                getElement().getStyle().clearFontStyle();
                if (defaultText.equals(getText())) {
                    setText("");
                }
            }
        });

        addBlurHandler(new BlurHandler() {
            @Override
            public void onBlur(BlurEvent event) {
                if ("".equals(getText())) {
                    resetDefaultText();
                }
            }
        });
    }

    public String getDefaultText() {
        return defaultText;
    }

    @Override
    public void setText(String text) {
        if (text == null) {
            super.setText(getDefaultText());
        } else {
            getElement().getStyle().clearColor();
            getElement().getStyle().clearFontStyle();
            super.setText(text);
        }
    }

    public String getText() {

        return super.getText();
    }

    /**
     * This is override so that the editor framework will not get the default
     * value but the actual null value when the default text is in the box.
     */
    @Override
    public String getValue() {
        try {
            return getValueOrThrow();
        } catch (ParseException e) {
            return null;
        }

    }

    @Override
    public void setValue(String value) {
        setText(value);
    }

    /**
     * This is overridden from the parent class because this is 
     * how the editor gets the value.
     */
    public String getValueOrThrow() throws ParseException {
        if (defaultText.equals(super.getValueOrThrow())) {
            return null;
        }
        return super.getValueOrThrow();
    }


    /**
     * Reset the text box to the default text.
     */
    public void resetDefaultText() {
        setText(defaultText);
        getElement().getStyle().setColor(TEXTBOX_DISABLED_COLOR);
        getElement().getStyle().setFontStyle(FontStyle.ITALIC);
    }
}

然后,您可以在模板中设置类似的属性.

Then in the template you can set properties like this.

<w:DefaultTextBox defaultText="name" ui:field="nameTextBox" />

这也将与setter一起使用,您可以设置属性而不必使用@UiConstructor,但就我而言,我想确保该类没有空的构造函数.

This will also work with setters, you can set properties without having to use the @UiConstructor but in my case I wanted to make sure that there was no empty constructor for this class.

这篇关于如何在GWT中为BlurEvent正确添加UiHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:19