2012-05-04 133 views
12

是否有任何知道Google Web Toolkit文本输入窗口小部件在空字段时会在字段内显示消息?如何将占位符添加到GWT文本输入字段

E.g.第一个名字字段会说:'输入你的名字',当用户开始输入时,标签被删除以显示输入的文字。

你会怎么做呢?

丹尼尔

回答

31

这并不在一个文本对我的工作:

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

我认为这是一个HTML5特性。

+0

太棒了。完美的作品。 – supercobra

1

有了这个解决方案可以定义粘结剂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> 
1

我会去与一个UI框架,做这一切为你(及以上)。

我在我所有的项目中使用GWT-引导3和很乐意看:https://github.com/gwtbootstrap3/gwtbootstrap3

如果你不知道的引导,最好是快速通过一本书bootstrap3的走,所以你现在怎么样网格系统和其他东西的作品。当你使用gwt-bootstrap 3时,你会头痛不已。

另一个你可以看看的是https://github.com/GwtMaterialDesign/gwt-material这是Google Material的封装。我自己并没有使用它,但我认为这不是一个错误的选择。

+0

placeHolder自带GWT材质。 –

相关问题