14

是否知道 Google Web Toolkit 文本输入小部件会在字段为空时显示一条消息?

例如,名字字段会说:“输入你的名字”,当用户开始输入时,标签被删除以显示输入的文本。

你会怎么做?

丹尼尔

4

3 回答 3

34

这在 textarea 中为我工作:

yourInputField.getElement().setPropertyString("placeholder", "enter your first name");

我认为这是一个 HTML5 功能。

于 2012-05-04T15:36:14.347 回答
1

使用此解决方案,您可以在 binder xml 中定义任何其他属性:

解决方案:

public class MorePropertiesTextArea extends TextArea {
    private String moreProperties;

    public MorePropertiesTextArea() {}

    public void setMoreProperties(String moreProperties) {
        for (Entry<String, String> entry : parse(moreProperties).entrySet()) {
            getElement().setAttribute(entry.getKey(), entry.getValue());
        }
        this.moreProperties = moreProperties;
    }


    public String getMoreProperties() {
        return moreProperties;
    }

    private Map<String, String> parse(String moreProperties) {
        String[] pairStrings = moreProperties.split(";");
        HashMap<String, String> map = new HashMap<>();
        for (String pairString : pairStrings) {
            String[] pair = pairString.split("=");
            if(pair.length != 2)
                throw new IllegalArgumentException("at " + pair + " while parsing " + moreProperties + 
                        ". Use format like: 'spellcheck=false; placeholder=Write something here...'");
            map.put(pair[0].trim(), pair[1]);
        }
        return map;
    }

}

UI 活页夹 xml:

<p2:MultiLineTextArea ui:field="taMultiLine" styleName="form-control muliLine"  
            moreProperties="spellcheck=false; placeholder=Write something here... " />

结果html:

<textarea class="form-control muliLine" spellcheck="false" placeholder="Write something here..."></textarea>
于 2016-07-11T21:04:44.363 回答
1

我会选择一个 UI 框架来为你(以及更多)做这一切。

我在所有项目中都使用 GWT-Bootstrap 3,对此我感到非常高兴:https ://github.com/gwtbootstrap3/gwtbootstrap3

如果您不了解 bootstrap,最好快速阅读一本 bootstrap3 书,了解 Grid 系统和其他东西的工作原理。比使用 gwt-bootstrap 3 时有先见之明。

您可以查看的另一个是https://github.com/GwtMaterialDesign/gwt-material,它是 Google Material 的包装器。我自己没用过,但我认为这不是一个糟糕的选择。

于 2016-07-12T06:28:58.520 回答