【问题标题】:Codename One - Custom Calendar day button代号一 - 自定义日历日按钮
【发布时间】:2017-08-16 07:23:24
【问题描述】:

我的应用程序保存约会并将它们显示在多按钮列表中,但我还使用 GUI 构建器创建了一个带有日历的屏幕,因为我想创建一个“日历视图”,以便用户可以查看日历和:

1- 查看突出显示至少一个约会的每个日期(自定义 UIID)

2- 在每个日历日查看他当天的约会次数以及课程日期。

我读到我可以通过覆盖 updateButtonDayDate()createDay() 来做到这一点,但我不知道如何在我的代码中做到这一点。能给我举个例子吗?

【问题讨论】:

  • Felipe,我对 CN1 应用程序有类似的问题,如果您能找到解决方案,我会对代码感兴趣。能发个sn-p吗?

标签: java codenameone


【解决方案1】:

为了在 Calendar 类中覆盖 updateButtonDayDate()createDay(),您需要创建一个扩展代号日历的自定义日历(确保扩展 com.codename1.ui.calendar 而不是 java.util)。

在创建自定义日历类之后,您将不得不废弃 GUI 构建器日历并在代码中创建一个。查看下面代码后面的用法示例:

import com.codename1.ui.Button;
import com.codename1.ui.Calendar;
import static com.codename1.ui.Component.CENTER;

public class CustomCalendar extends Calendar {

    @Override
    protected Button createDay() {
        //Customize your button here
        Button day = new Button();
        day.setAlignment(CENTER);
        day.setUIID("CalendarDay");
        day.setEndsWith3Points(false);
        day.setTickerEnabled(false);
        return day;
    }

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

要使用它,请根据您的自定义版本声明一个新日历:

CustomCalendar myCalendar = new CustomCalendar();
myContainer.add(myCalendar);

您可以在 CustomCalendar 中尝试各种操作,直到找到可行的方法。 Ctrl + 单击日历以检查源并了解可以覆盖和不能覆盖的内容。

您甚至可以创建扩展 Container 的日历版本(尽管不推荐)。

【讨论】:

    【解决方案2】:

    要在代号日历中选择或显示多个日期,一种选择是将日期添加到列表中并根据列表项格式化日期按钮:

     cal = new Calendar() {
    
            @Override
            protected void updateButtonDayDate(Button dayButton, int currentYear, int currentMonth, int day  ) {
    
                list.add(1);
                list.add(12);
                list.add(13);
                list.add(14);
                list.add(21);
    
    
                for (int day_Number : list) {
    
                    if (day_Number == day) {
    
                        dayButton.setText("" + day);
                        dayButton.setUIID("mycalender-day");
    
                    }
    
            }
    
        };
    

    css文件包含格式样式:

    mycalender-day {
    
    border: 1px solid whitesmoke;
    color:orange;
    font-family:  "native:MainRegular";
    font-size: 7pt;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 2022-10-16
      相关资源
      最近更新 更多