【问题标题】:Codenameone - Calendar createDay() NavigationCodenameone - 日历 createDay() 导航
【发布时间】:2017-09-21 16:17:21
【问题描述】:

我使用 CN1(基于 CN1 的标准表单模板)对我的应用程序进行了手工编码。主要用于使用日历进行约会应用程序(我有理由不使用 Picker)。

这是我的主要表单类

public class celebriesta {

private Form current;
private Resources theme;

private Form home;

public void init(Object context) {
    theme = UIManager.initFirstTheme("/theme");

    // Enable Toolbar on all Forms by default
    Toolbar.setGlobalToolbar(true);

    // Pro only feature
    Log.bindCrashProtection(true);
}

public void start() {
    if (current != null) {
        current.show();
        return;

    }
        home = new Form("Home", BoxLayout.y());
        mainCalendar Calendar = new mainCalendar();
        home.addComponent(Calendar);
        Calendar.setUIID("Calendar");


    //Create Form1 and Form2 and set a Back Command to navigate back to the home Form        
    Form form1 = new Form("Form1");
    setBackCommand(form1);
    Form form2 = new Form("Form2");
    setBackCommand(form2);
    Form form3 = new Form("Form3");
    setBackCommand(form3);


    //Add navigation commands to the home Form

    NavigationCommand cmd1 = new NavigationCommand("Form1");
    cmd1.setNextForm(form1);
    home.getToolbar().addCommandToSideMenu(cmd1);

    NavigationCommand cmd2 = new NavigationCommand("Form2");
    cmd2.setNextForm(form2);
    home.getToolbar().addCommandToSideMenu(cmd2);

    NavigationCommand cmd3 = new NavigationCommand("Form3");
    cmd3.setNextForm(form3);
    Calendar.createDay().pressed();
    Calendar.createDay().released();
    Calendar.createDay().setCommand(cmd3);

    //Add Edit commands to the home Form context Menu
    Image im = FontImage.createMaterial(FontImage.MATERIAL_MODE_EDIT, UIManager.getInstance().getComponentStyle("Command"));
    Command edit = new Command("", im) {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.out.println("Editing");
        }
    };
    home.getToolbar().addCommandToRightBar(edit);
    home.show();
}

protected void setBackCommand(Form f) {
    Command back = new Command("") {

        @Override
        public void actionPerformed(ActionEvent evt) {
            home.showBack();
        }

    };
    Image img = FontImage.createMaterial(FontImage.MATERIAL_ARROW_BACK, UIManager.getInstance().getComponentStyle("TitleCommand"));
    back.setIcon(img);
    f.getToolbar().addCommandToLeftBar(back);
    f.getToolbar().setTitleCentered(true);
    f.setBackCommand(back);
}

public void stop() {
    current = getCurrentForm();
}

public void destroy() {
}}

我已经相应地覆盖了日历类

public class mainCalendar extends Calendar { @Override
    protected Button createDay(){ Button day = new Button();
    Image im = FontImage.createMaterial(FontImage.MATERIAL_MODE_EDIT, UIManager.getInstance().getComponentStyle("Command"));
    day.setIcon(im);

    return day;

   }     
    @Override
protected void updateButtonDayDate(Button dayButton, int currentMonth, int day) {
    //Customize day values 
    dayButton.setText("" +day);
}}

主窗体设法进入窗体 1 和 2(侧菜单)。我知道 Form 3 确实存在,但不确定为什么它没有从 createDay() 中“到达”。而且我怀疑主表单中此代码的某处有问题

Calendar.createDay().pressed();
Calendar.createDay().released();
Calendar.createDay().setCommand(cmd3);

需要建议和/或帮助。

【问题讨论】:

  • 你有什么问题?

标签: codenameone


【解决方案1】:

查看创建自定义日历日组件的sample code here。您不需要以下代码:

Calendar.createDay().pressed();
Calendar.createDay().released();
Calendar.createDay().setCommand(cmd3);

日历日的按下和释放是通过actionListener 处理的,如果您使用自定义日期组件,您可以通过覆盖bindDayListener()addDayActionListener() 如果您使用默认日期按钮来实现。一个例子是:

Calendar.addDayActionListener(evt -> {
    //show your next form here
});

除非您需要高级定制,否则我看不出继承 Calendar 类的意义。

【讨论】:

  • 谢谢钻石。我会试试你的建议。
  • TQ 再次钻石。该代码有效。我的代码错误,也是因为我没有相应地更新 CN1 库。傻我...
  • @buzzin_hornets 很高兴它起作用了......请为答案投票
  • 顺便说一下,正确的 Java 编码标准是以小写开头的变量名、以大写开头的类名和以小写开头的方法/函数名。 mainCalendar 类应变为 MainCalendarCalendar 变量应为 calendarcal
  • 所有大写的常量
猜你喜欢
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 2017-10-16
  • 1970-01-01
  • 2018-01-02
  • 2021-10-31
相关资源
最近更新 更多