Back to project page androidui.
The source code is released under:
MIT License
If you think the Android project androidui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package course.examples.UI.datepicker; //from w ww . j a v a 2s . co m import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; public class DatePickerFragmentActivity extends Activity implements OnDateSetListener { private TextView mDateDisplay; private Button mPickDate; private int mYear; private int mMonth; private int mDay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Capture UI elements mDateDisplay = (TextView) findViewById(R.id.dateDisplay); mPickDate = (Button) findViewById(R.id.pickDate); // Set an OnClickListener for the Change the Date Button mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Create a new DatePickerFragment DialogFragment newFragment = new DatePickerFragment(); // Display DatePickerFragment newFragment.show(getFragmentManager(), "DatePicker"); } }); } public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } // Update the date String in the TextView private void updateDisplay() { mDateDisplay.setText(new StringBuilder() // Month is-0 based so add 1 .append(mMonth + 1).append("-").append(mDay).append("-") .append(mYear).append(" ")); } public static class DatePickerFragment extends DialogFragment implements OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Set the current date in the DatePickerFragment final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } // Callback to DatePickerActivity.onDateSet() to update the UI @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { ((OnDateSetListener) getActivity()).onDateSet(view, year, monthOfYear, dayOfMonth); } } }