【发布时间】:2011-06-22 13:56:30
【问题描述】:
我想在 GWT 日期框内设置水印/占位符。我知道如何使用 onFocus 和 onBlur 在普通的 TextBox 中设置水印/占位符。我认为在 DateBox 中这样做会比较相似。设置文本当前看起来像这样,但什么都不做。
Datebox box = new DateBox();
box.getTextBox().setText("mm/dd/yyyy");
这有什么不可行的原因吗?
【问题讨论】:
我想在 GWT 日期框内设置水印/占位符。我知道如何使用 onFocus 和 onBlur 在普通的 TextBox 中设置水印/占位符。我认为在 DateBox 中这样做会比较相似。设置文本当前看起来像这样,但什么都不做。
Datebox box = new DateBox();
box.getTextBox().setText("mm/dd/yyyy");
这有什么不可行的原因吗?
【问题讨论】:
box.getTextBox().setValue("mm/dd/yyyy");
【讨论】:
setText 继承自 HasText,实际上只是将值传递给 setValue。
我想你在这里真正谈论的是能够设置占位符文本。我为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");。
【讨论】: