标签:
一. TimePicker
1.创建TimePickerFragment
package com.panjn.userinterface; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.widget.TimePicker; import android.widget.Toast; import java.text.DateFormat; import java.util.Calendar; /** * Created by panjianning on 2016/7/6. */ public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int min = calendar.get(Calendar.MINUTE); /** * Params * context the parent context * listener the listener to call when the time is set * hourOfDay the initial hour * minute the initial minute * is24HourView whether this is a 24 hour view or AM/PM */ return new TimePickerDialog(getActivity(), this, hour, min, true); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user } }
2.显示TimePickerFragment
public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker"); }
二. DatePicker
1.创建DatePickerFragement
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user } }
2.显示DatePickerFragment
public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); }
标签:
原文地址:http://www.cnblogs.com/mmyz-sysu-panjn/p/Pickers.html