您可以尝试继承 DropDownButton,为触发按钮添加一个 getter(在类的末尾),如下所示:
public class CustomDropdownButton extends DropdownBase {
private Button trigger;
/**
* Creates a DropdownButton without a caption.
*/
public CustomDropdownButton() {
super("div");
addStyleName("btn-group");
}
/**
* Creates a DropdownButton with the given caption.
*
* @param caption
* the button's caption
*/
public CustomDropdownButton(String caption) {
this();
setText(caption);
}
/**
* {@inheritDoc}
*/
@Override
protected IconAnchor createTrigger() {
trigger = new Button();
trigger.setCaret(true);
return trigger;
}
/**
* Sets the button's size.
*
* @param size
* the button's size
*/
public void setSize(ButtonSize size) {
trigger.setSize(size);
}
/**
* Sets the button's type.
*
* @param type
* the button's type
*/
public void setType(ButtonType type) {
trigger.setType(type);
}
/**
* Sets the button's icon.
*
* @param type
* the icon's type
*/
@Override
public void setIcon(IconType type) {
setBaseIcon(type);
}
/**
* {@inheritDoc}
*/
@Override
public void setBaseIcon(BaseIconType type) {
trigger.setBaseIcon(type);
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return trigger.addClickHandler(handler);
}
/**
* {@inheritDoc}
*/
@Override
public void setIconSize(IconSize size) {
trigger.setIconSize(size);
}
/**
* {@inheritDoc}
*/
@Override
public void setCustomIconStyle(String customIconStyle) {
trigger.setCustomIconStyle(customIconStyle);
}
/**
* {@inheritDoc}
*/
@Override
public void setIconPosition(IconPosition position) {
trigger.setIconPosition(position);
}
public Button getButton(){
return trigger;
}
}
现在您将拥有 uiBinder xml :
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:b="urn:import:com.github.gwtbootstrap.client.ui"
xmlns:g='urn:import:com.google.gwt.user.client.ui'
xmlns:c='urn:import:com.example.packageWithCustomDropDown'
...
<c:CustomDropdownButton text="Test" ui:field="dropdownButton">
<g:FlowPanel ui:field="contentPanel"/>
</b:CustomDropdownButton>
现在您可以在按钮上调用 click 函数:dropDownButton.getButton().click();
我没有测试它,但它应该可以工作。
希望能帮助到你。:)