我正在创建一个供用户填写的调查,其中一个要求是实现一个进度条,在用户回答问题时填满。
我无法完全理解的错误是列表框。如果我选择一个值,计数器就会上升,但如果我选择另一个值,计数器会再次增加,此时它应该保持相同的值。
下面的基本示例:
- 选择值
- 真的
- 错误的
完成百分比:0%(默认下拉列表,“选择值”作为默认值)
- 真的
- 选择值
- 错误的
完成百分比:2%
- 错误的
- 选择值
- 真的
完成百分比:4%(应该是 2%,因为问题已经回答)
这是更改处理程序的代码:
final ValueChangeHandler<Boolean> booleanChangeHandler = new ValueChangeHandler<Boolean>()
{
@Override
public void onValueChange(final ValueChangeEvent<Boolean> event)
{
Boolean value = event.getValue();
changeAnsweredQuestionTotal(String.valueOf(value));
}
};
这会改变进度条。我还用它来检查文本框是否已填写。
public void changeAnsweredQuestionTotal(String value)
{
//Window.alert(value);
boolean isEmpty = (value == null) || value.equalsIgnoreCase("Null") ? true : value.isEmpty();
Integer answered;
Integer totalQuestions;
String text = this.getQuestionsAnswered().getText().substring(0, 2);
text= text.replaceAll("\\s+", "");
answered = text.isEmpty() ? 0 : Integer.valueOf(text);
answered = (answered < 0) ? 0 : answered;
totalQuestions = this.getTotalQuestions().getValue().isEmpty() ? 0 : Integer.valueOf(this.getTotalQuestions().getValue());
String result;
if (isEmpty)
{
answered = answered - 1;
answered = (answered < 0) ? 0 : answered;
result = answered + " questions answered. ";
}
else
{
if (!value.equalsIgnoreCase("null"))
{
answered = answered > totalQuestions ? totalQuestions : answered + 1;
}
result = answered + " questions answered. ";
}
Integer percent = (answered*100)/totalQuestions;
percent = (percent > 100) ? 100 : percent;
}
任何帮助将不胜感激。