请看下面的代码
<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);
}
}
当“Toast”工作时,它显示月份减少 1 个值。这意味着如果我选择 MARCH(第 3 个月)并单击按钮,它将月份显示为 2。这是为什么呢?请帮忙!