【问题标题】:GWT DateBox Watermark/PlaceholderGWT 日期框水印/占位符
【发布时间】:2011-06-22 13:56:30
【问题描述】:

我想在 GWT 日期框内设置水印/占位符。我知道如何使用 onFocus 和 onBlur 在普通的 TextBox 中设置水印/占位符。我认为在 DateBox 中这样做会比较相似。设置文本当前看起来像这样,但什么都不做。

    Datebox box = new DateBox();
    box.getTextBox().setText("mm/dd/yyyy");

这有什么不可行的原因吗?

【问题讨论】:

    标签: gwt watermark datebox


    【解决方案1】:
    box.getTextBox().setValue("mm/dd/yyyy");
    

    【讨论】:

    • 即使尝试了这个,DateBox 也没有显示任何文本。出于好奇,TextBox 中的 setText 和 setValue 有什么区别(抱歉这个菜鸟问题)?
    • 我刚刚试了一下,效果不错,你是不是写的有点过了?
    • @chris setText 继承自 HasText,实际上只是将值传递给 setValue
    【解决方案2】:

    我想你在这里真正谈论的是能够设置占位符文本。我为TextBox 元素here before 发布了一个解决方案。该过程将非常相似:

    public class DateField extends DateBox {
    
      String placeholder = "";
    
      /**
       * Creates an empty DateField.
       */
      public DateField() {}
    
      /**
       * Gets the current placeholder text for the date box.
       * 
       * @return the current placeholder text
       */
      public String getPlaceholder() {
          return placeholder;
      }
    
      /**
       * Sets the placeholder text displayed in the date box.
       * 
       * @param placeholder the placeholder text
       */
      public void setPlaceholder(String text) {
          placeholder = (text != null ? text : "");
          getElement().setPropertyString("placeholder", placeholder);
      }
    }
    

    然后用DateField 对象替换您的DateBox 对象,您只需调用someDateField.setPlaceholder("mm/dd/yyyy");

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2012-08-23
      • 1970-01-01
      • 2017-02-04
      • 2011-05-04
      • 2018-10-14
      • 1970-01-01
      • 2021-09-03
      • 2015-09-06
      相关资源
      最近更新 更多