代理一个日期选择器问题

时间:2017-04-11 08:22:13

标签: java arrays oop codenameone

我创建了一个自定义日历,每当我点击日历按钮时,我都会尝试调用选择器,但它对我不起作用,以下是我的应用程序代码,它无法正常工作。那么如何修改此代码以便我能够从ActionEvent调用选择器?

    public class PivDisplayCalendar extends Container {


    int length =37;
     private ComboBox year;
     private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     private static final String[] LABELS = {"Su", "M", "Tu", "W", "Th", "F", "Sa"};

     private ArrayList<Button> allButtons = new ArrayList<Button>();
     //Button newButton = new Button("");

    public PivDisplayCalendar(){

        super(new BoxLayout(BoxLayout.Y_AXIS));
            Container calendarTitle = new Container(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER));
            Container title = new Container(new GridLayout(1,7));
            Container days = new Container(new GridLayout(6, 7));
            Container calendarTitleCopy = new Container(new GridLayout(1, 1));
                calendarTitleCopy.setUIID("CalendarTitleCopy");
            this.addComponent(calendarTitleCopy);
            this.addComponent(title);
            this.addComponent(days);


            Button prevMonth = new Button("<");
            Button nextMonth = new Button(">");
            SpanLabel monthYear = new SpanLabel("Month " + " Year");
            calendarTitle.add(BorderLayout.WEST, prevMonth);
            calendarTitle.add(BorderLayout.CENTER, monthYear);
            calendarTitle.add(BorderLayout.EAST, nextMonth);
            calendarTitleCopy.add(calendarTitle);
            Button dayButton= new Button();




           if(UIManager.getInstance().isThemeConstant("calTitleDayStyleBool", false)) {
                title.setUIID("CalendarTitleArea");
                days.setUIID("CalendarDayArea");
            }
            for (int iter = 0; iter < DAYS.length; iter++) {
                title.addComponent(createDayTitle(iter));
            }
            for (int iter = 1; iter < length; iter++) {
               dayButton = new Button(""+iter);

                    dayButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {
                            Log.p("Action event triggered");
                            Picker datePicker = new Picker();
                            datePicker.setType(Display.PICKER_TYPE_DATE);

                            //Button b1 = (Button)(evt.getActualComponent());
                            //Log.p( b1.getText() );
                        }
                    });

               allButtons.add(dayButton);
                days.addComponent(dayButton);
                if (iter <= 7) {
                    dayButton.setNextFocusUp(year);
                }
            }              
    }


     protected Label createDayTitle(int day) {
        String value = getUIManager().localize("Calendar." + DAYS[day], LABELS[day]);
        Label dayh = new Label(value, "Label");
        dayh.setEndsWith3Points(false);
        dayh.setTickerEnabled(false);
        return dayh;
    }
}

这是我的应用程序屏幕截图: -

enter image description here

以下方法用于事件处理: -

for (int iter = 1; iter < length; iter++) {
               dayButton = new Button(""+iter);

                    dayButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {
                            Log.p("Action event triggered");
                            Picker datePicker = new Picker();
                            datePicker.setType(Display.PICKER_TYPE_DATE);

                            //Button b1 = (Button)(evt.getActualComponent());
                            //Log.p( b1.getText() );
                        }
                    });

               allButtons.add(dayButton);
                days.addComponent(dayButton);
                if (iter <= 7) {
                    dayButton.setNextFocusUp(year);
                }
            }

1 个答案:

答案 0 :(得分:2)

您的代码会创建一个Picker组件,但不会将其添加到任何表单或显示它。可能你正在寻找的是Display.showNativePicker(),因为这只会弹出一个原生的选择器对话框。

您应首先检查平台是否支持原生选择器(仅iOS和Android具有原生选择器),并创建自己的对话框,其中包含DateSpinner组件。查看source for the Picker class以查看其如何执行此操作的示例。