2017-04-11 115 views
1

我创建了一个自定义日历,我尝试在每次点击日历按钮时调用选择器,但它不能为我工作,以下是我的应用程序代码,它无法正常工作。那么如何修改这段代码,以便我能够从ActionEvent中调用选择器?Codename一个日期选择器问题

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); 
       } 
      } 
+0

它在做什么?有没有错误信息/异常?请包含日历按钮操作代码。 – lupz

+0

我已经更新了这个问题。 @lupz –

+0

啊,对不起!我想我一见钟情就错了。但我仍然感到困惑......就我所了解的代码而言,每个月的每一天都有一个按钮(如屏幕截图所示)。所以你想在用户选择一个日期(选择/触摸一天按钮)后显示日期选择器?你不能立即设置/使用选定的日期/日期吗?无论如何,行动代码可能会缺少一些实际显示选择器的内容。至少在[codenameOne Picker javadoc-example](https://www.codenameone.com/javadoc/com/codename1/ui/spinner/Picker.html)中,他们将选取器添加到表单并调用show()在那。 – lupz

回答

2

你的代码创建一个Picker组件,但不会将其添加到任何形式或显示它。可能你要找的是Display.showNativePicker(),因为这只会弹出一个本地选取器对话框。

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

+0

谢谢史蒂夫帮助我很多我正在为Android和IOS做这个项目,所以这将是我的项目的理想解决方案。 @steve hannah –