2010-10-15 74 views
15

我使用GWT和UiBinder的我的应用程序,我试图做到这一点风俗UiBinder的属性部件

<g:TextBox ui:field="searchBox" styleName="{style.searchBox}" placeholder="search" />

但自定义属性placeholder将无法​​工作,因为没有在TextBox一个setPlaceholder方法 - 我需要这样的:

searchBox.getElement().setAttribute("placeholder", "search");

早在Java代码。关于如何在UiBinder本身做到这一点的想法?我想我可以将它改为正常的输入元素,并尝试获取参考及其价值,但我宁愿不走那条路。

+0

Afaik这是不可能的(正如你所说,你需要一个setter方法)。为什么不在调用'initWidget(uiBinder.createAndBindUi(this))'后立即在相应视图类的构造函数中设置属性?编辑:或编写自己的占位符功能,也可以在旧版浏览器上使用。 – z00bs 2010-10-15 07:49:15

+0

'initWidget'就是我现在调用'setAttribute'的地方......我不想将演示文稿移出XML文件...... – 2010-10-15 14:25:32

回答

14

如何创建自定义SearchBox,延长TextBox与方法setPlaceholder(String placeholder)

然后在UiBinder的:

<custom:SearchBox ui:field="searchBox" styleName="{style.searchBox}" placeholder="search" />

+2

这可能是我最终会在这种情况下做的事情,但我发现使用基于属性的HTML 5功能变得越来越困难:( – 2010-10-15 14:24:26

8

关于在此之后一年就被问,我有必要使用自定义属性(占位符,特别是)。所以我写了以下自定义TextField类,它扩展了TextBox,保留了GWT TextBox的所有基础功能,包括处理程序等。希望有人在他们的搜索中绊倒这一点。 :)

public class TextField extends TextBox { 

    String placeholder = ""; 

    /** 
    * Creates an empty text box. 
    */ 
    public TextField() {} 

    /** 
    * Gets the current placeholder text for the text box. 
    * 
    * @return the current placeholder text 
    */ 
    public String getPlaceholder() { 
     return placeholder; 
    } 

    /** 
    * Sets the placeholder text displayed in the text box. 
    * 
    * @param placeholder the placeholder text 
    */ 
    public void setPlaceholder(String text) { 
     placeholder = (text != null ? text : ""); 
     getElement().setPropertyString("placeholder", placeholder); 
    } 
} 
+3

您不需要将占位符作为java变量存储,您可以直接使用DOM值。 – logan 2012-11-06 22:47:37