DialogFragment 实现TimePickerDialog

在实现 onCreateDialog(int id) 发现方法已过时,新的方法是 DialogFragment,FragmentManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
int hour, minute;

public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(getActivity(), this, hour, minute, false);//返回一个TimePickerDialog
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
hour = hourOfDay;
minute = minuteOfHour;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");
Date date;
Calendar calendar = Calendar.getInstance();//Date构造方法过时,Calendar转Date
calendar.set(0,0,0,hour,minute);
date = calendar.getTime();
String strDate = simpleDateFormat.format(date);

Toast.makeText(getActivity(), "you have selected" + strDate,
Toast.LENGTH_SHORT).show();
}
}

显示 Dialog

1
2
DialogFragment dialogFragment = new TimePickerFragment();
dialogFragment.show(getSupportFragmentManager(),"timePicker");