我在頁面上有幾個文本框,用戶可以輸入一些數字值;然而,盡我所能,我不能用默認值填充這些文本框 - 具體而言,我希望0.0
在頁面加載時都顯示。GWT - setText和setValue在初始加載時不能用於TextBox
這是我如何創建它們,我曾嘗試 -
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
?
您可以發佈您的「KSTextBox」代碼嗎? – enrybo 2013-02-14 20:06:01
@enrybo - 我發佈了這段代碼。 – 2013-02-14 20:32:46
對不起,找不到問題。 – enrybo 2013-02-14 21:41:31