0

テキスト フィールドとラジオ ボタンのリストを表示する for ループがあります。テキストフィールドを読んだり、チェックされているラジオボタンを見つけたりできるように、ウィジェットを参照する最良の方法は何ですか? これが私のループです

    for(int x = 0; x<getLoopCount(); x++)
    {
        answerTable.setWidget(x,0, new Label("Answer:"));
        answerTable.setWidget(x,1, new TextBox());
        answerTable.setWidget(x,2, new RadioButton(""));
    }

各ウィジェットを識別して参照できるようにする方法はありますか?

4

3 に答える 3

0

その場しのぎの回答をさせてください。そのため、構文ではなくアルゴリズムのアイデアを気にしてください。

  1. GWT ボタンを拡張します。
    abstract class MyButton
    extends Button{
      // provide the appropriate constructor in impl class,
      // especially if using uibinder

      abstract public void helloDolly(... args ...);
    }
  1. MyButton を使用して、これらすべてのボタンをインスタンス化します。

    MyButton[] buttons = {
      new MyButton(){
         public void helloDolly(... args ...){
           Window.alert("allo allo #1");
         }
      },
    
      new MyButton(){
         public void helloDolly(... args ...){
           Window.alert("allo allo #2");
         }
      },
    
      // blah blah black sheep ....
    }
    
  2. ハンドラーを定義するときに clickEvent.getSource() を使用します。

    buttons[i].addEventHandler(
      new ClickHandler(ClickEvent click){
        Object src = click.getSource();
        if (src !instanceOf MyButton){
           throw new MyAngryException("For goodness' sake, pls use MyButton");
           // or ignore
           return;
        }
    
        ((MyButton)src).helloDolly(... args ...);
      }
    )
    
于 2013-02-24T17:10:07.337 に答える
0

次のように、3 つのウィジェットを複合ウィジェットにグループ化することをお勧めします。

class AnswerComposite extends Composite {
    private final Label label;
    private final TextBox textBox;
    private final RadioButton radioButton;

    public AnswerComposite() {
        label = new Label("Answer:");
        textBox = new TextBox();
        radioButton = new RadioButton("answerGroup");

        HorizontalPanel contentPanel = new HorizontalPanel();
        contentPanel.add(label);
        contentPanel.add(textBox);
        contentPanel.add(radioButton);

        initWidget(contentPanel);
    }

    public String getText() {
        return textBox.getValue();
    }

    public boolean isSelected() {
        return radioButton.getValue();
    }
}

次に、それらをパネルに追加したり、次のようにリストに配置したりできます。

VerticalPanel answersPanel = new VerticalPanel();
List<AnswerComposite> answerComposites = new ArrayList<AnswerComposite>();

for (int i = 0; i < getLoopCount(); i++) {
    AnswerComposite answerComposite = new AnswerComposite();
    answersPanel.add(answerComposite);
    answerComposites.add(answersComposite);
}

ウィジェットのチェックは非常に簡単になります。

answerComposites.get(i).getText();
answerComposites.get(i).isSelected();

ValueChangeHandleraをsに追加することもおそらく便利RadioButtonです(enryboの回答を参照)。

于 2013-02-24T16:36:11.933 に答える
0

ValueChangeHandlerそれらを作成するRadioButtonときに、 を に追加できます。

for(int x = 0; x<getLoopCount(); x++){

    answerTable.setWidget(x,0, new Label("Answer:"));
    answerTable.setWidget(x,1, new TextBox());

    RadioButton rb = new RadioButton("");
    rb.addValueChangeHandler(new ValueChangeHandler(){
        @Override
        void onValueChange(ValueChangeEvent<Boolean> event){
            // Do something
        }
    });

    answerTable.setWidget(x,2, rb);

}

がチェックされているValueChangeEvent場合にのみ発生しRadioButtonます。RadioButton同じグループ内の別のチェックボックスがオンになっている場合は発火しません。

ValueChangeHandler作成中に を追加しているのでRadioButton、ID を作成しなくても、それで何をすべきかを知っておく必要があります。

于 2013-02-24T13:14:52.487 に答える