【问题标题】:Custom attributes in UiBinder widgetsUiBinder 小部件中的自定义属性
【发布时间】:2011-04-25 18:23:27
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • Afaik 这是不可能的(正如你所说,你需要一个 setter 方法)。为什么不直接在调用initWidget(uiBinder.createAndBindUi(this))之后在对应视图类的构造函数中设置属性呢?编辑:或编写您自己的占位符功能,该功能也适用于旧版浏览器。
  • initWidget 之后我现在正在调用setAttribute...我不想将演示文稿移出XML 文件...

标签: java gwt uibinder


【解决方案1】:

使用方法setPlaceholder(String placeholder) 创建扩展TextBox 的自定义SearchBox 怎么样?

然后在 UiBinder 中:

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

【讨论】:

  • 这可能是我在这种情况下最终要做的事情,但我发现使用基于属性的 HTML 5 功能变得越来越困难:(
【解决方案2】:

在被问到这个问题大约一年后,我需要使用自定义属性(特别是占位符)。因此,我编写了以下自定义 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);
  }
}

【讨论】:

  • 您也不需要将占位符存储为 java 变量。你可以直接使用 DOM 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
相关资源
最近更新 更多