- Back to Home »
- Android »
- How to use DatePickerDialog
Tuesday, June 26, 2012
If you want a component to the current date,
But if EditText lets the user fill in the time that is really troublesome.
The format of the user error,
Just use the trouble can be avoided DatePickerDialog.
First of all, you first create a Button with a TextView
XML is as follows
Then you will initialize
And then the inside of the Activity overwrite onCreateDialog,
Above showDialog method is the call we have established the Dialog
Then the above have one setDateFormat method,
This method can set you want to set the date format
As a result, you want to date will become a "year - month - day" presentation on the View
In the beginning you will see this picture
Button will pop up when you press
The selected date can be seen
Download
http://uploadingit.com/file/micbhmqw6qqlcnvy/DateDemo.zip
ps. if you do not want to jump out of DatePickerDialog Choose a time, press the "Cancel", the system remember the date you choose,
Inside in the onClick method with datePickerDialog.updateDate () this method can instantly update
But if EditText lets the user fill in the time that is really troublesome.
The format of the user error,
Just use the trouble can be avoided DatePickerDialog.
First of all, you first create a Button with a TextView
XML is as follows
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date" android:id="@+id/dateButton" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dateText" /> </LinearLayout>
Then you will initialize
private Button dateButton; private Calendar calendar; private int mYear, mMonth, mDay; private TextView dateText; private DatePickerDialog datePickerDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = Calendar.getInstance(); mYear = calendar.get(Calendar.YEAR); mMonth = calendar.get(Calendar.MONTH); mDay = calendar.get(Calendar.DAY_OF_MONTH); dateText = (TextView)findViewById(R.id.dateText); dateButton = (Button)findViewById(R.id.dateButton); dateButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { showDialog(0); datePickerDialog.updateDate(mYear, mMonth, mDay); } }); }
And then the inside of the Activity overwrite onCreateDialog,
Above showDialog method is the call we have established the Dialog
@Override protected Dialog onCreateDialog(int id) { datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { mYear = year; mMonth = month; mDay = day; dateText.setText("你設定的日期為"+setDateFormat(year,month,day)); } }, mYear,mMonth, mDay); return datePickerDialog; }
onDateSet method when you press OK, it will jump into this time we will select all save it, Then the above have one setDateFormat method,
This method can set you want to set the date format
private String setDateFormat(int year,int monthOfYear,int dayOfMonth){ return String.valueOf(year) + "-" + String.valueOf(monthOfYear + 1) + "-" + String.valueOf(dayOfMonth); }
As a result, you want to date will become a "year - month - day" presentation on the View
In the beginning you will see this picture
Button will pop up when you press
The selected date can be seen
Download
http://uploadingit.com/file/micbhmqw6qqlcnvy/DateDemo.zip
ps. if you do not want to jump out of DatePickerDialog Choose a time, press the "Cancel", the system remember the date you choose,
Inside in the onClick method with datePickerDialog.updateDate () this method can instantly update
@ Override
public void onClick (View view) {
showDialog (0);
datePickerDialog.updateDate (mYear, mMonth, mDay);
}