3

I am create dynamic number of radio buttons in my GWT

      public void createTestList(ArrayList<Test> result){
    for(int i =0 ; i<result.size();i++){
                    int id = result.get(i).getTestId();
        RadioButton rd = new RadioButton("group", result.get(i).getTestType());
        verticalPanel.add(rd);
    }

where Test is my Entity class ..

I am getting 4 different types of radio buttons in my view , Now if i select any one of the radio button, first I need to get the id of the selected Radio button , how can this be possible ?

secondly How will i check that which one of the multiple radio button is selected ?

Thanks

4

2 回答 2

2

您需要检查每个单选按钮上的 public java.lang.Boolean getValue() 是否被选中。

于 2012-05-24T06:08:58.920 回答
0

可以添加点击处理程序并更新选定的单选按钮变量:

choiceItemKind = new VerticalPanel();
ArrayList<String> kinds = new ArrayList<String>();
kinds.add(...);
kinds.add(...);

choiceItemKind.clear();

ClickHandler choiceClickHandler = new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        addItemKindSelectedLabel = ((RadioButton) event.getSource()).getText();
    }
};

for (String label : kinds)
{
    RadioButton radioButton = new RadioButton("kind", label);
    //radioButton.setTitle("Tooltyp");
    if (label.equals(addItemKindSelectedLabel))
        radioButton.setValue(true);
    radioButton.addClickHandler(choiceClickHandler);
    choiceItemKind.add(radioButton);
}
...
addItemKindSelectedLabel = "";
...
if (!addItemKindSelectedLabel.isEmpty())
    ...;

upd:设置选定的单选按钮而不重建:

for (int i = 0; i < choiceItemKind.getWidgetCount(); i++)
{
    RadioButton radioButton = (RadioButton) choiceItemKind.getWidget(i);
    radioButton.setValue(radioButton.getText().equals(addItemKindSelectedLabel));
}
于 2015-05-13T08:12:41.627 回答