java GWT RadioButton 更改处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13153801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:41:09  来源:igfitidea点击:

GWT RadioButton Change Handler

javagwtradio-buttonlistener

提问by Arya

I have a poll widget with RadioButton choices and Label votes

我有一个带有 RadioButton 选项和标签投票的投票小部件

  1. When user selects a choice, choice votes should +1;
  2. When another choice selected, old choice votes should -1 and new choice votes should +1.
  1. 当用户选择一个选项时,选择票应为+1;
  2. 当另一种选择选择时,旧选择投票应该--1和新选择选票应该+1。

I used ValueChangeHandler for this:

我为此使用了 ValueChangeHandler:

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+"");
}

When user selects new choice, older choice listener should jump in else statement, but it won't (Only +1 part works). What should i do?

当用户选择新选项时,旧选项监听器应该跳转到 else 语句中,但它不会(只有 +1 部分有效)。我该怎么办?

回答by user1332981

It says in the RadioButton javadocthat you won't receive a ValueChangeEvent when a radio button is cleared. Unfortunately, this means you will have to do all bookkeeping yourself.

它在RadioButton javadoc中说,当清除单选按钮时,您将不会收到 ValueChangeEvent。不幸的是,这意味着您必须自己进行所有簿记。

As an alterative to creating your own RadioButtonGroup class as suggested on the GWT issue tracker, you could consider doing something like this:

作为根据 GWT 问题跟踪器的建议创建自己的 RadioButtonGroup 类的替代方法,您可以考虑执行以下操作:

private int lastChoice = -1;
private Map<Integer, Integer> votes = new HashMap<Integer, Integer>();
// Make sure to initialize the map with whatever you need

Then when you initialize the radio buttons:

然后当您初始化单选按钮时:

List<RadioButton> allRadioButtons = new ArrayList<RadioButton>();

// Add all radio buttons to list here

for (RadioButton radioButton : allRadioButtons) {
    radioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> e) {
                updateVotes(allRadioButtons.indexOf(radioButton));
        });
}

The updateVotes method then looks something like this:

然后 updateVotes 方法看起来像这样:

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
}

Not very elegant, but it should do the job.

不是很优雅,但它应该可以完成这项工作。

回答by Anders R. Bystrup

There is an open defect on this specific problem over at the GWT issue tracker. The last comment has a suggestion, basically it appears you need to have changehandlers on all radiobuttons and keep track of the groupings yourself...

GWT 问题跟踪器上有一个关于这个特定问题的公开缺陷。最后一条评论有一个建议,基本上看来您需要在所有单选按钮上都有更改处理程序并自己跟踪分组...

Cheers,

干杯,