DatePicker 使用的 DefaultDateMonthSelector (com.google.gwt.user.datepicker.client) 不会公开这些小部件,因此无法直接禁用这些按钮。
要实现此功能,请编写一个新的 DateMonthSelector(您可以从 DefaultMonthSelector 的源代码http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/ gwt/user/datepicker/client/DefaultMonthSelector.java)。例如,
public final class LimitedMonthSelector extends MonthSelector {
// Keep the PushButtons disabled
// Modified from the original DefaultMonthSelector
@Override
protected void setup() {
// Set up backwards.
backwards = new PushButton();
backwards.getUpFace().setHTML("«");
backwards.setStyleName(css().previousButton());
backwards.setEnabled(false); // Disable the back button
forwards = new PushButton();
forwards.getUpFace().setHTML("»");
forwards.setStyleName(css().nextButton());
forwards.setEnabled(false); // Disable the forward button
// Set up grid.
grid = new Grid(1, 3);
grid.setWidget(0, 0, backwards);
grid.setWidget(0, 2, forwards);
CellFormatter formatter = grid.getCellFormatter();
formatter.setStyleName(0, 1, css().month());
formatter.setWidth(0, 0, "1");
formatter.setWidth(0, 1, "100%");
formatter.setWidth(0, 2, "1");
grid.setStyleName(css().monthSelector());
initWidget(grid);
}
}
然后在 DatePicker 实现中使用此 MonthSelector 实现。喜欢,
public class LimitedDatePicker extends DatePicker {
public MonthYearDatePicker() {
super(new LimitedMonthSelector(), new DefaultCalendarView(), new CalendarModel());
}
}
这个新的 LimitedDatePicker 将禁用其后退/前进按钮。