我在页面上有几个文本框,用户可以在其中输入一些数值;但是,尽我所能,我无法用默认值填充这些文本框 - 特别是我希望0.0
在页面加载时同时显示。
以下是我创建它们的方法以及我尝试过的方法 -
GroupSection engineering_group = new GroupSection();
KSTextBox engrDesignTextBox = new KSTextBox();
engrDesignTextBox.setWidth("2.875em");
//engrDesignTextBox.setWatermarkText("0.0"); ==> this works, but not what I need
//engrDesignTextBox.setText("0.0"); ==> this doesn't work
engrDesignTextBox.setValue("0.0"); // doesn't work either
KSTextBox engrScienceTextBox = new KSTextBox();
engrScienceTextBox.setWidth("2.875em");
//engrScienceTextBox.setWatermarkText("0.0"); ==> this works, but not what I need
//engrScienceTextBox.setText("0.0"); ==> this doesn't work
engrScienceTextBox.setValue("0.0"); // doesn't work either
我在想我需要附加一个“onload”事件侦听器,然后在那里尝试 setText?对于应该相当简单的事情来说,这似乎有点矫枉过正。
顺便说一句,我为这两个文本框附加了 onBlurHandlers,它们按预期工作(见下面的代码)
0.0
如果用户在文本框为空时单击或跳出文本框,则以下代码将简单地插入。
engrDesignTextBox.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent blurEvent) {
if(((KSTextBox)blurEvent.getSource()).getText().length() < 1) {
((KSTextBox)blurEvent.getSource()).setText("0.0");
}
}
});
engrScienceTextBox.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent blurEvent) {
if(((KSTextBox)blurEvent.getSource()).getText().length() < 1) {
((KSTextBox)blurEvent.getSource()).setText("0.0");
}
}
});
编辑:这里的要求是我在 KSTextBox 中定义 setText 和 setValue 方法的方式
public class KSTextBox extends TextBox implements HasWatermark {
.
.
.
@Override
public void setText(String text) {
String oldValue = super.getText();
if(hasWatermark) {
if(text == null || (text != null && text.isEmpty())){
super.setText(watermarkText);
addStyleName("watermark-text");
watermarkShowing = true;
}
else{
super.setText(text);
removeStyleName("watermark-text");
watermarkShowing = false;
}
}
else{
super.setText(text);
}
ValueChangeEvent.fireIfNotEqual(this, oldValue, text);
}
@Override
public void setValue(String value) {
if(hasWatermark) {
if(value == null || (value != null && value.isEmpty())){
super.setValue(watermarkText);
addStyleName("watermark-text");
watermarkShowing = true;
}
else{
super.setValue(value);
removeStyleName("watermark-text");
watermarkShowing = false;
}
}
else{
super.setValue(value);
}
}
那么,回到最初的问题,我最初如何将这些文本框的值设置为0.0
?