Java GWT单选按钮更改处理程序
我有一个带有单选按钮选择和标签投票的投票小部件Java GWT单选按钮更改处理程序,java,gwt,radio-button,listener,Java,Gwt,Radio Button,Listener,我有一个带有单选按钮选择和标签投票的投票小部件 当用户选择一项选择时,选择票数应为+1 当选择另一个选项时,旧选项投票应为-1,新选项投票应为+1 我使用ValueChangeHandler进行此操作: valueRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() { @Override public void onValueChange(ValueCh
valueRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> e) {
if(e.getValue() == true)
{
System.out.println("select");
votesPlusDelta(votesLabel, +1);
}
else
{
System.out.println("deselect");
votesPlusDelta(votesLabel, -1);
}
}
});
private void votesPlusDelta(Label votesLabel, int delta)
{
int votes = Integer.parseInt(votesLabel.getText());
votes = votes + delta;
votesLabel.setText(votes+"");
}
valueRadioButton.addValueChangeHandler(新的ValueChangeHandler(){
@凌驾
价值变更时的公共无效(价值变更事件e){
如果(如getValue()==true)
{
System.out.println(“选择”);
votesPlusDelta(votesLabel,+1);
}
其他的
{
System.out.println(“取消选择”);
votesPlusDelta(votesLabel,-1);
}
}
});
专用void votesPlusDelta(标签voteslab,int delta)
{
int vows=Integer.parseInt(votesLabel.getText());
票数=票数+增量;
votesLabel.setText(票数+“”);
}
当用户选择新的选项时,旧的选项侦听器应该跳入else语句,但它不会(只有+1部分有效)。我该怎么办 在这个特定问题上有一个公开的缺陷。最后一条评论有一个建议,基本上你需要在所有单选按钮上设置ChangeHandler,并自己跟踪分组 干杯,它在中说,当清除单选按钮时,您将不会收到ValueChangeEvent。不幸的是,这意味着你将不得不自己做所有的簿记工作
作为GWT问题跟踪器中建议的创建自己的RealButtoGROUP类的一种替代方法,您可以考虑这样做:
private int lastChoice = -1;
private Map<Integer, Integer> votes = new HashMap<Integer, Integer>();
// Make sure to initialize the map with whatever you need
private void updateVotes(int choice) {
if (votes.containsKey(lastChoice)) {
votes.put(lastChoice, votes.get(lastChoice) - 1);
}
votes.put(choice, votes.get(choice) + 1);
lastChoice = choice;
// Update labels using the votes map here
}
不是很优雅,但它应该可以完成任务。问题已转移到github:
private void updateVotes(int choice) {
if (votes.containsKey(lastChoice)) {
votes.put(lastChoice, votes.get(lastChoice) - 1);
}
votes.put(choice, votes.get(choice) + 1);
lastChoice = choice;
// Update labels using the votes map here
}