7

当我创建几个单选按钮 ( ) 并使用先前选择的单选按钮new Button(parent, SWT.RADIO)以编程方式设置选择时,它也保持选中状态。radioButton5.setSelection(true)我是否必须遍历同一组的所有其他单选按钮才能取消选择它们,或者是否有更简单的选择?提前致谢。

4

3 回答 3

9

不幸的是,您必须遍历所有选项。当您的 UI 第一次出现时,BN_CLICKED就会触发一个事件。如果您的ShellGroup任何单选按钮容器未使用SWT.NO_RADIO_GROUP选项创建,则调用以下方法:

void selectRadio () 
{
    Control [] children = parent._getChildren ();
    for (int i=0; i<children.length; i++) {
        Control child = children [i];
        if (this != child) child.setRadioSelection (false);
    }
    setSelection (true);
}

所以本质上 eclipse 本身依赖于遍历所有单选按钮并切换它们的状态。

每次您手动选择单选按钮时,BN_CLICKED都会触发事件并因此自动切换。

当您使用时,不会触发button.setSelection(boolean)任何事件。BN_CLICKED因此没有自动切换单选按钮。

检查org.eclipse.swt.widgets.Button课程以获取更多详细信息。

于 2011-04-29T22:56:44.167 回答
1

同一组合中的单选按钮将作为一个组。一次只能选择一个单选按钮。这是一个工作示例:

    Composite composite = new Composite(parent, SWT.NONE);

    Button btnCopy = new Button(composite, SWT.RADIO);
    btnCopy.setText("Copy Element");
    btnCopy.setSelection(false);

    Button btnMove = new Button(composite, SWT.RADIO);
    btnMove.setText("Move Element");
于 2013-09-17T13:35:46.990 回答
-2

这应该自动发生。你是如何创建按钮的?他们是同一个父母吗?父母是否使用 NO_RADIO_GROUP 样式?

于 2011-04-30T02:56:28.040 回答