15

我正在为我的应用程序使用 GWT 和 UiBinder,我正在尝试这样做

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

但是自定义placeholder属性不起作用,因为没有setPlaceholder方法 on TextBox- 我需要这样做:

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

回到java代码。关于如何在 UiBinder 本身中执行此操作的任何想法?我想我可以将其更改为普通输入元素并尝试获取引用及其值,但我宁愿不走那条路。

4

2 回答 2

14

如何创建使用方法SearchBox扩展的自定义?TextBoxsetPlaceholder(String placeholder)

然后在 UiBinder 中:

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

于 2010-10-15T10:11:49.183 回答
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);
  }
}
于 2011-10-14T15:06:24.250 回答