简体   繁体   中英

Android Calendar handling days of week

I am making a calendar for android, I picked up this tutorial on this site: http://www.androidhub4you.com/2012/10/custom-calendar-in-android.html

I'm having trouble changing some code lines, three problems:

  • one: the color that indicates the today's day is appearing every month. I would like to only appear in the current month.

  • two: change the color of the day on Sunday and Monday to red.

  • three: Do not allow clicks in the days of Sunday and Monday


Code:

Problems one and two:

    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();
        }



    }

Problem three:

  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(); } } 

Problem 1:

Currently you are checking if the number of the day ( i ) is the same as the current day number of the month ( getCurrentDayOfMonth() ), so, for example; today Sept-2 the number of the day is 2, regardless the month. You have to be more strict about your condition. You have to check not just the day but also the month where you are.

Probably you will have to change your code from:

if (i == getCurrentDayOfMonth()) {

to something like:

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


Problem 2:

The values of i will be from 1 to 30&something (or 28/29 in some cases). The value of the constant Calendar.SUNDAY is 1. So, in your code you are asking if the current value of i is equal to 1. You have to see if the current value of i on the current month that you are displaying is SUNDAY, how? this post can hep you: how-to-determine-day-of-week-by-passing-specific-date

Probably you will have to change your code from:

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

to something like:

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


Problem 3:

Here you have to check, after click on the day of the calendar if the selected button, is Sunday or Monday. In the code that you posted before, you can see that they are creating a Date object, (parseDate). You can check if this Date is MONDAY/SUNDAY and based on that start or not the activity..

From your code to:

 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(); } } 

Where anotherNewMethodToSeeIfTheDateIsSundayOrMonday should be something like this:

 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; } 

From here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM