The effect is as follows:
** Old rules have the source code at the end, the steps are as follows:
- Call the Calendar class to get the year, month and day **
Important: At the end of the month you will notice that the month is missing a 1, this is normal, the API says that the original value of the month is 0, add a 1 yourself.
2. Put the date, month, and day values in the DatePickerDialog
The source code is as follows:
Layout file **
<TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="216dp" Android :textSize="20sp"/>Copy the code
Java file:
public class MainActivity extends Activity { private TextView time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); time.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showtime(); }}); } private void initView() { time = (TextView) findViewById(R.id.time); } private void showtime() { Calendar calendar = Calendar.getInstance(); Int mYear = Calendar.get (Calendar.year); Int mMonth = Calendar.get (Calendar.month); // Month is added by one, which is originally 0. If you don't add the meeting, you'll lose a month. int mDay = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, Int i2) {time. The setText (I + "years" + (i1 + 1) + "month" + i2 + ", "); }}, mYear,mMonth, mDay); // Put the date in the DatePickerDialog and pass the value to the datepickerdialog.show (); // Display dialog}}Copy the code