【问题标题】:Android Calendar handling days of weekAndroid日历处理星期几
【发布时间】:2015-09-03 07:10:36
【问题描述】:

我正在为 android 制作日历,我在这个网站上找到了这个教程:http://www.androidhub4you.com/2012/10/custom-calendar-in-android.html

我在更改某些代码行时遇到了问题, 三个问题:

  • one:表示今天的颜色每个月都会出现。我想只出现在当月。

  • 二:将周日和周一的一天颜色改为红色。

  • 三:不允许在周日和周一的日子里点击


代码:

问题一和二:

    for (int i = 1; i <= daysInMonth; i++) {
        Log.d(currentMonthName, String.valueOf(i) + " " 
            + getMonthAsString(currentMonth) + " " + yy);

        if (i == getCurrentDayOfMonth()) {
            list.add(String.valueOf(i) + "-BLUE" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        }
        //else if not working
        else if (i == Calendar.SUNDAY){
            list.add(String.valueOf(i) + "-RED" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        } else {
            list.add(String.valueOf(i) + "-WHITE" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        }
    }

问题三:

    Override
    public void onClick(View view) {

        // i cant implement the if statament for that :(

        String date_month_year = (String) view.getTag();

        Intent intent = new Intent(getBaseContext(), func_agenda.class);
          intent.putExtra("user_auth", "Paulo");
          intent.putExtra("user_date", date_month_year);
          startActivity(intent);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

        } catch (ParseException e) {

              e.printStackTrace();
        }



    }

【问题讨论】:

    标签: android calendar


    【解决方案1】:

    问题一:

    当前您正在检查日数 (i) 是否与当前月数 (getCurrentDayOfMonth()) 相同,因此,对于例子;今天 9 月 2 日,天数为 2,与月份无关。你必须对你的条件更加严格。您不仅要查看日期,还要查看您所在的月份。

    您可能需要从以下位置更改您的代码:

    if (i == getCurrentDayOfMonth()) {
    

    类似于:

    if (i == getCurrentDayOfMonth() && mm == _calendar.get(Calendar.MONTH) ) { //Check if mm is from zero to 11. If not the condition should be mm-1 ==... 
    


    问题2:

    i 的值从 1 到 30&something(或在某些情况下为 28/29)。常量Calendar.SUNDAY 的值为 1。因此,在您的代码中,您要询问 i 的当前值是否等于 1。您必须查看当前显示的当月 i 的当前值是否为 SUNDAY , 如何?这篇文章可以帮助你:how-to-determine-day-of-week-by-passing-specific-date

    您可能需要从以下位置更改您的代码:

    else if (i == Calendar.SUNDAY){
    

    类似于:

    else if ( newMethodToGetDayOfWeek(i,mm,yy) == Calendar.SUNDAY){
    


    问题3:

    在这里您必须检查,如果选择的按钮是星期天或星期一,则单击日历中的日期后。在您之前发布的代码中,您可以看到他们正在创建一个 Date 对象 (parseDate)。您可以检查此日期是否为星期一/星期日,并根据该开始或不是活动..

    从您的代码到:

    Override
    public void onClick(View view) {
    
        String date_month_year = (String) view.getTag();
    
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());
    
            if( anotherNewMethodToSeeIfTheDateIsSundayOrMonday( parsedDate ) == false )
            {
                Intent intent = new Intent(getBaseContext(), func_agenda.class);
                intent.putExtra("user_auth", "Paulo");
                intent.putExtra("user_date", date_month_year);
                startActivity(intent);
            } 
        } catch (ParseException e) {
              e.printStackTrace();
        }
    }
    

    anotherNewMethodToSeeIfTheDateIsSundayOrMonday 应该是这样的:

    private bool anotherNewMethodToSeeIfTheDateIsSundayOrMonday(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int doWeek = cal.get(Calendar.DAY_OF_WEEK)
        if( doWeek == Calendar.SUNDAY || doWeek == Calendar.MONDAY )
        {
            return true;
        }
    
        return false;
    }
    

    来自here

    【讨论】:

    • 感谢您的快速回复!我去看看,让你知道!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多