创建日期或时间选择窗口需要弹出Dialog的时候,Activity类的showDialog方法已经弃用了,而推荐使用的是DialogFragment,本文总结一下其具体用法.
(一)日期选择器
1、创建MyDatePickerDialog类,继承自DatePickerDialog类,实现构造方法,重写onDateChanged方法:
1 import android.app.DatePickerDialog; 2 import android.content.Context; 3 import android.widget.DatePicker; 4 5 public class MyDatePickerDialog extends DatePickerDialog { 6 7 public MyDatePickerDialog (Context context, OnDateSetListener callBack, 8 int year, int monthOfYear, int dayOfMonth) { 9 super(context, callBack, year, monthOfYear, dayOfMonth); 10 11 this.setTitle("选择任务的日期"); 12 this.setButton2("取消", (OnClickListener)null); 13 this.setButton("确定", this); //setButton和this参数组合表示这个按钮是确定按钮 14 15 } 16 17 @Override 18 public void onDateChanged(DatePicker view, int year, int month, int day) { 19 super.onDateChanged(view, year, month, day); 20 this.setTitle("选择任务的日期"); 21 } 22 23 }
注:隐藏日期选择器的“日”选择项的方法:在MyDatePickerDialog的构造方法中添加一个参数:代表日期选择器类型的整型参数,比如0代表年月日都显示,1表示只显示年和月等,然后用如下代码来隐藏“日”选择项(隐藏年月的方法同理,时间选择器也同理):
1 // 获取当前系统的语言 2 Locale locale = context.getResources().getConfiguration().locale; 3 String language = locale.getLanguage(); 4 // 隐藏日选择栏 5 if (language.endsWith("zh")) { 6 ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)) 7 .getChildAt(0)).getChildAt(2).setVisibility(View.GONE); 8 } else { 9 ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)) 10 .getChildAt(0)).getChildAt(1).setVisibility(View.GONE); 11 }
2、创建DatePickerFragment类,继承自DialogFragment类并实现DatePickerDialog.OnDateSetListener接口,重写其onCreateDialog和onDateSet方法:
1 import java.util.Calendar; 2 3 import android.app.Dialog; 4 import android.app.DialogFragment; 5 import android.os.Bundle; 6 import android.widget.DatePicker; 7 import android.app.DatePickerDialog; 8 9 public class DatePickerFragment extends DialogFragment implements 10 DatePickerDialog.OnDateSetListener { 11 @Override 12 public Dialog onCreateDialog(Bundle savedInstanceState) { 13 final Calendar c = Calendar.getInstance(); 14 int year = c.get(Calendar.YEAR); 15 int month = c.get(Calendar.MONTH); 16 int day = c.get(Calendar.DAY_OF_MONTH); 17 return new MyDatePickerDialog(getActivity(), this, year, month, day); 18 } 19 20 @Override 21 public void onDateSet(DatePicker view, int year, int month, int day) { 22 23 } 24 25 }
3、在活动中显示日期选择器
1 DatePickerFragment datePickerFrg = new DatePickerFragment() { 2 @Override 3 public void onDateSet(DatePicker view, int year, int month, 4 int day) { 5 Log.d("DateSet","选择的日期是:" + year +"-" + (month + 1) + "-" + day); } 6 }; 7 datePickerFrg.show(getFragmentManager(), "datePickerFrg");
效果:
(二)时间选择器
时间选择器的使用其实和日期选择器差不多.
1、创建MyTimePickerDialog类,继承自TimePickerDialog,实现构造方法,重写onTimeChanged方法:
1 import android.app.TimePickerDialog; 2 import android.content.Context; 3 import android.content.DialogInterface.OnClickListener; 4 import android.widget.TimePicker; 5 6 public class MyTimePickerDialog extends TimePickerDialog { 7 8 public MyTimePickerDialog (Context context, OnTimeSetListener callBack, 9 int hourOfDay, int minute, boolean is24HourView) { 10 super(context, callBack, hourOfDay, minute, is24HourView); 11 12 this.setTitle("选择任务的时间"); 13 this.setButton2("取消", (OnClickListener)null); 14 this.setButton("确定", this); //setButton和this参数组合表示这个按钮是确定按钮 15 } 16 17 @Override 18 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 19 super.onTimeChanged(view, hourOfDay, minute); 20 this.setTitle("选择任务的时间"); 21 } 22 23 }
2、创建TimePickerFragment类,继承自DialogFragment类并实现TimePickerDialog.OnTimeSetListener接口,重写其onCreateDialog和onTimeSet方法:
1 public class TimePickerFragment extends DialogFragment implements 2 TimePickerDialog.OnTimeSetListener { 3 4 @Override 5 public Dialog onCreateDialog(Bundle savedInstanceState) { 6 final Calendar calendar = Calendar.getInstance(); 7 int hour = calendar.get(Calendar.HOUR_OF_DAY); 8 int minute = calendar.get(Calendar.MINUTE); 9 10 return new MyTimePickerDialog(getActivity(), this, hour, minute, 11 DateFormat.is24HourFormat(getActivity())); 12 } 13 14 @Override 15 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 16 17 } 18 }
3、在活动中显示时间选择器:
1 TimePickerFragment timePickerFrg = new TimePickerFragment() { 2 @Override 3 public void onTimeSet(android.widget.TimePicker view, 4 int hourOfDay, int minute) { 5 Log.d("TimeSet", "选择的时间是:" + hourOfDay + ":" + minute); 6 }; 7 }; 8 timePickerFrg.show(getFragmentManager(), "timePickerFrg");
效果: