在Calendar Android App中显示错误的月份

时间:2015-03-27 14:58:29

标签: java android xml date calendar

以下是Calendar Android App的代码。

MainActivity.java

package com.example.calendar;

import android.os.Build;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public class MainActivity extends Activity {
    CalendarView calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //sets the main layout of the activity
        setContentView(R.layout.activity_main);

        //initializes the calendar view
        initializeCalendar();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") public void initializeCalendar() {
        calendar = (CalendarView) findViewById(R.id.calendar);

        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);

        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);

        //The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));

        //sets the color for the dates of an unfocused month.
        calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));

        //sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));

        //sets the color for the vertical bar shown at the beginning and at the end of the selected date.
        calendar.setSelectedDateVerticalBar(R.color.darkgreen);

        //sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
                       //show the selected date as a toast
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp" 
        android:background="@color/white"
        android:orientation="vertical" >

        <CalendarView
            android:id="@+id/calendar"
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calendar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="transparent" type="color">#00FFFFFF</item>
    <item name="white" type="color">#FFFFFF</item>
    <item name="green" type="color">#4499CC00</item>
    <item name="darkgreen" type="color">#FF669900</item>
</resources>

它突出显示日历中的正确日期,但可以选择以dd / mm / yyyy格式显示日期。那里显示错误的月份。如果月份是3月,则显示2015年2月27日,而不是显示2015年3月27日。任何人都可以说代码中有什么不对。

请帮忙。提前谢谢。

0 个答案:

没有答案