创建日期或时间选择窗口需要弹出Dialog的时候,Activity类的showDialog方法已经弃用了,而推荐使用的是DialogFragment,本文总结一下其具体用法.

  (一)日期选择器

  1、创建MyDatePickerDialog类,继承自DatePickerDialog类,实现构造方法,重写onDateChanged方法:

 import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker; public class MyDatePickerDialog extends DatePickerDialog { public MyDatePickerDialog (Context context, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth); this.setTitle("选择任务的日期");
this.setButton2("取消", (OnClickListener)null);
this.setButton("确定", this); //setButton和this参数组合表示这个按钮是确定按钮 } @Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
this.setTitle("选择任务的日期");
} }

  注:隐藏日期选择器的“日”选择项的方法:在MyDatePickerDialog的构造方法中添加一个参数:代表日期选择器类型的整型参数,比如0代表年月日都显示,1表示只显示年和月等,然后用如下代码来隐藏“日”选择项(隐藏年月的方法同理,时间选择器也同理):

      // 获取当前系统的语言
Locale locale = context.getResources().getConfiguration().locale;
String language = locale.getLanguage();
       // 隐藏日选择栏
if (language.endsWith("zh")) {
((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0))
.getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
} else {
((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0))
.getChildAt(0)).getChildAt(1).setVisibility(View.GONE);
}

  2、创建DatePickerFragment类,继承自DialogFragment类并实现DatePickerDialog.OnDateSetListener接口,重写其onCreateDialog和onDateSet方法:

 import java.util.Calendar;

 import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import android.app.DatePickerDialog; public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new MyDatePickerDialog(getActivity(), this, year, month, day);
} @Override
public void onDateSet(DatePicker view, int year, int month, int day) { } }

  3、在活动中显示日期选择器

         DatePickerFragment datePickerFrg = new DatePickerFragment() {
@Override
public void onDateSet(DatePicker view, int year, int month,
int day) {
Log.d("DateSet","选择的日期是:" + year +"-" + (month + 1) + "-" + day); }
};
datePickerFrg.show(getFragmentManager(), "datePickerFrg");

  效果:

  (二)时间选择器

  时间选择器的使用其实和日期选择器差不多.

  1、创建MyTimePickerDialog类,继承自TimePickerDialog,实现构造方法,重写onTimeChanged方法:

 import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.widget.TimePicker; public class MyTimePickerDialog extends TimePickerDialog { public MyTimePickerDialog (Context context, OnTimeSetListener callBack,
int hourOfDay, int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView); this.setTitle("选择任务的时间");
this.setButton2("取消", (OnClickListener)null);
this.setButton("确定", this); //setButton和this参数组合表示这个按钮是确定按钮
} @Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
super.onTimeChanged(view, hourOfDay, minute);
this.setTitle("选择任务的时间");
} }

  2、创建TimePickerFragment类,继承自DialogFragment类并实现TimePickerDialog.OnTimeSetListener接口,重写其onCreateDialog和onTimeSet方法:

 public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener { @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE); return new MyTimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
} @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { }
}

  3、在活动中显示时间选择器:

         TimePickerFragment timePickerFrg = new TimePickerFragment() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.d("TimeSet", "选择的时间是:" + hourOfDay + ":" + minute);
};
};
timePickerFrg.show(getFragmentManager(), "timePickerFrg");

  效果:

随机推荐

  1. flume A simple example

    http://flume.apache.org/FlumeUserGuide.html A simple example

  2. Spring 3 MVC and JSR303 @Valid example

    http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/ ———————————————————————————— ...

  3. 第一百九十一节,jQuery EasyUI 入门

    jQuery EasyUI 入门 学习要点: 1.什么是 jQuery EasyUI 2.学习 jQuery EasyUI 的条件 3.jQuery EasyUI 的功能和优势 4.其他的 UI 插件 ...

  4. (phpQuery)对网站产品信息采集代码的优化

    a.要采集的源链接: http://www.prospecbio.com/Recombinant_Proteins/ b.具体要求:

  5. poj 2112(二分+多重匹配)

    题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...

  6. Apache thrift - 使用,内部实现及构建一个可扩展的RPC框架

    本文首先介绍了什么是Apache Thrift,接着介绍了Thrift的安装部署及如何利用Thrift来实现一个简单的RPC应用,并简单的探究了一下Thrift的内部实现原理,最后给出一个基于Thri ...

  7. tomcat NIO配置

    1.tomcat NIO配置 今天在查看日志时发现tomcat的Socket连接方式为bio,于是我想既然有bio那肯定有nio.果然,一查就发现tomcat在6.0之后就可以配置nio的方式.nio ...

  8. Android无线测试之—Genymotion配置过程中常见问题

    一.前提条件: 已经部署好了Android UiAutomator测试环境. 二.在部署Genymotion时遇到了两类问题: 1.通过eclipse打开一个模拟设备,然后将编译好的jar包push到 ...

  9. std::condition_variable(2)复习

    #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yie ...

  10. (int)()和int()强制类型转换

    (int)()这个是c语言的强制转换,众所知周 int(),这种理解靠谱点 Class A { A(int i){} } 调用int类型的构造函数然后转换的 其实都可以转换.