我现在正在努力解决以下问题: 我不明白为什么方法setStatus()不起作用。方法setButton()检查字段“status”中的各个元素是否为“true”,如果这一切都必须激活一个按钮。不幸的是,staus字段值不会改变。
我想补充一点,textBox.setTitle()和textBox.addStyleName效果很好。
这是我的代码:
package com.mg;
import java.util.ArrayList;
import com.comarch.spr.client.UserServlet;
import com.comarch.spr.client.UserServletAsync;
import com.gargoylesoftware.htmlunit.javascript.host.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
public class Validator {
private ArrayList<ValidationElement> listOfElements = new ArrayList<Validator.ValidationElement>();
private Button button;
public int i = 0;
public Validator(ArrayList<ValidationElement> listOfElements, Button button) {
this.listOfElements = listOfElements;
this.button = button;
}
public void createElement0(TextBox textbox, String regex, int type, UserServletAsync userServlet ) {
ValidationElement element = new ValidationElement(textbox, regex, type, userServlet);
listOfElements.add(element);
}
public void createElement1(TextBox textbox, String regex, int type ) {
ValidationElement element = new ValidationElement(textbox, regex, type);
listOfElements.add(element);
}
public void addElement(ValidationElement element) {
listOfElements.add(element);
}
public void setButton() {
int sizeOfList = listOfElements.size();
int check = 0;
for(ValidationElement element : listOfElements) {
element.validate();
if (element.status == true)
check = check + 1;
}
if (check != 0)
button.setEnabled(true);
else
button.setEnabled(false);
}
//========================== VALIDATION ELEMENT CLASS===========================
public class ValidationElement {
public TextBox textBox = new TextBox();
public boolean status;
public String regex;
public int type; // 0 - validateFromDatabase() , || 1 - checkRegEx()
public UserServletAsync userServlet;
public ValidationElement(TextBox textbox, String regex, int type ) {
this.textBox = textbox;
this.regex = regex;
this.type = type;
}
public ValidationElement(TextBox textbox, String regex, int type, UserServletAsync userServlet) {
this.textBox = textbox;
this.regex = regex;
this.type = type;
this.userServlet = userServlet;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
//========================== VALIDATION FUNCTIONS =========================================
public void validate() {
if (type == 0) {
validateFromDatabase();
}
else if(type == 1)
checkRegEx();
}
public void validateFromDatabase() {
textBox.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
userServlet.validateUser(textBox.getText(), new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
}
public void onSuccess(Boolean result) {
boolean resultOfCallback = result;
if(resultOfCallback){
textBox.addStyleName("invalid");
textBox.setTitle("error");
}
else if (text.matches(regex) && !resultOfCallback) {
textBox.setStyleName("gwt-TextBox");
setStatus(true);
}
else if (text.matches(regex) && resultOfCallback) {
}
else {
textBox.addStyleName("invalid");
textBox.setTitle("error");
}
}
});
}
});
}
public void checkRegEx () {
textBox.addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
setStatus(false);
}
else {
setStatus(true);
}
isValid();
}
});
textBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
textBox.addStyleName("invalid");
textBox.setTitle("error");
setStatus(false);
}
else
{
setStatus(true);
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("error");
}
}
});
}
public void isValid() {
if(this.status = true)
i=i+1;
}
}
}
答案 0 :(得分:0)
public void isValid() {
if(this.status = true)
i=i+1;
}
不应该是
public void isValid() {
if(this.status == true)
i=i+1;
}
第一种方法将状态设置为true,然后始终递增i
。
或者你可以做得更好:
public void isValid() {
if(this.status) {
i++;
}
}
此外,如果您正在使用Eclipse,则可以设置编译器选项以突出显示if
语句中的意外布尔分配作为编译错误。这是一个非常有用的选择。
答案 1 :(得分:0)
在类Validator中,有:
public int i = 0;
在ValidationElement类中,在Validator中,例如checkRegEx()方法,我把: 我++;
public void checkRegEx () {
textBox.addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
setStatus(false);
}
else {
setStatus(true);
}
isValid();
}
});
textBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
final String text = textBox.getText();
if(!text.matches(regex))
{
textBox.addStyleName("invalid");
textBox.setTitle("error");
setStatus(false);
}
else
{
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("ok");
i++;
}
}
});
}
当我想知道一切都好的时候。事实证明,类Validator中setButton()方法中的“i”值仍为零,而此块中的其他代码行运行良好:
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("ok");
答案 2 :(得分:0)
在status
被调用后的下一行检查validate()
,很可能只是太早。
只有在BlurEvent
发生后才能告诉状态更改。 TextBox更新你提到工作正常,因为这些似乎是在适当的时刻触发。
public void setButton() {
int sizeOfList = listOfElements.size();
int check = 0;
button.setEnabled(false); // reset button
for(ValidationElement element : listOfElements) {
element.validate();
// drop attempt to check status here
}
// drop attempt to enable button here
}
//...
{
button.setEnabled(true); // was: setStatus(true);
textBox.setStyleName("gwt-TextBox");
textBox.setTitle("error");
}
此外,我更愿意定义ValidationElement
静态:
public static class ValidationElement
// have quite a painful experience chasing subtle bugs
// related to named non-static inner classes