且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从DatePicker拾取月份时出错

更新时间:2022-10-17 17:58:29

这是因为它从0开始。



作为

0 - 一月



1 - 二月



2 - 三月



3 - 四月...


检查这个


月 - 设置的月份(0-11)兼容性日历



Please have a look at the following code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="afterDescendants"
    android:background="@drawable/background1"
    tools:context=".Form" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/dateTxt"
        android:textSize="32sp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        style="@style/Text.Strong.Blurry" 

         />

        <DatePicker
        android:id="@+id/datePick" 
        android:layout_below="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="4dp"
        />

        <Button
            android:id = "@+id/goBtn"
            android:layout_below="@+id/datePick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/goBtnTxt"
            android:onClick="goNext"
            />

</RelativeLayout>






package xxxx.xxxx;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

public class Form extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_form, menu);
        return true;
    }

    public void goNext(View view)
    {
        DatePicker datePicker = (DatePicker)findViewById(R.id.datePick);

        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int date = datePicker.getDayOfMonth();

        Toast.makeText(this, String.valueOf(year)+":"+String.valueOf(month)+":"+String.valueOf(date), Toast.LENGTH_LONG).show();

        //Intent intent = new Intent(Form.this,DisplayResult.class);
        //startActivity(intent);
    }

}

When the 'Toast' is working, it showing the month reducing 1 value. Which means if I select MARCH (3rd month) and click the button it is showing the month as 2. Why is this? Please help!

This is because it starts from 0.

As

0 - January

1 - February

2 - March

3 - April...

Check this.

month - The month that was set (0-11) for compatibility with Calendar.