Android DatePickerDialog and the DialogFragment

简介:

Introduction:

Starting with Honeycomb and continuing in Ice Cream Sandwich, the Android SDK has introduced several new UI concepts to support larger screens such as tablets. Perhaps the most notable new UI feature is theFragment. Along with the updates to move developers towards fragments and size-independent layouts, Google has deprecated the methods commonly used to display Dialogs from Activities (seeActivity.showDialog() and Activity.onCreateDialog()). These methods were convient; displaying any type of Dialog was possible by simply overridingonCreateDialog(). In order to provide a better user experience when displaying dialogs from Fragments, Google has provided theDialogFragment. I have yet to find any decent tutorials working with DialogFragments, butthis code sample provided in the DialogFragment reference suffices.

I began to utilize this sample to provide a DatePickerDialog via DialogFragment. There exists one glaring problem with this code sample and applying it to a DatePicker: We need callbacks! After the user selects a date, we need to do something with that date. I set off to work up a useful DialogFragment that displayed a DatePicker.

My Solution:

Based on the sample code provided by Google, I came with up with a reusable DialogFragment that provided all the necessary DatePicker functionality. I could have continued with their theme and added the callback interface to the static inner class, but this will be far too reusable to nest it in some later irrelevant Activity. I instead went with a first class….class approach to be a little more flexible. On with it!

Step 1: Create a new class

Create a new class which inherits from DialogFragment and define a newInstance() method. Not much goes on in newInstance(), just some initialization.

public class DateDialogFragment extends DialogFragment {
 
    public static String TAG = "DateDialogFragment";
 
    static Context sContext;
    static Calendar sDate;
    static DateDialogFragmentListener sListener;
 
    public static DateDialogFragment newInstance(Context context, int titleResource, Calendar date){
		DateDialogFragment dialog  = new DateDialogFragment();
 
	sContext = context;
        sDate = date;
 
	Bundle args = new Bundle();
	args.putInt("title", titleResource);
	dialog.setArguments(args);
	return dialog;
    }
}


Step 2: Implement onCreateDialog()

Here’s where we can return the DatePickerDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(sContext, dateSetListener, sDate.get(Calendar.YEAR), sDate.get(Calendar.MONTH), sDate.get(Calendar.DAY_OF_MONTH));
}



Note dateSetListener being passed as the callback, I will detail this later.

Step 3: Define callback interface

Our DateDialogFragment needs a way to report back to its calling Fragment or Activity so we define an interface with a single callback method.

public interface DateDialogFragmentListener{
    public void dateDialogFragmentDateSet(Calendar date);
}

We also give callers a setDateDialogFragmentListener() method.

public void setDateDialogFragmentListener(DateDialogFragmentListener listener){
    sListener = listener;
}


Here’s what it looks like from the Activity or Fragment.

//create a new DateDialogFragment
DateDialogFragment ddf = DateDialogFragment.newInstance(this, R.string.set_date, date);
 
//assign a new DateDialogFragmentListener
ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
    //fired when user selects date
    @Override
    public void dateDialogFragmentDateSet(Calendar date) {
        // update the fragment
        mDateDetailFragment.updateDate(date);
    }
});


Step 4: Connect listeners

Now that we have defined a listener interface for the calling Activity or Fragment to receive notifications from our custom DateDialogFragment, we need to connect our DateDialogFragment to theDatePickerDialog we have created inside of it. I alluded to this in Step 2, where thedateSetListener object was being passed in the DatePickerDialog constructor.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(sContext, dateSetListener, sDate.get(Calendar.YEAR), sDate.get(Calendar.MONTH), sDate.get(Calendar.DAY_OF_MONTH));
}


dateSetListener is a DatePickerDialog.OnDateSetListener, the callback interface given to us by the default AndroidDatePickerDialog. In our DateDialogFragment class, we implement this interface and simply fire our own DateDialogFragmentListener.dateDialogFragmentDateSet() (see Step 3), passing along the date from theDatePickerDialog.OnDateSetListener. This completes the marriage between our DateDialogFragment and the DatePickerDialog within.


private DatePickerDialog.OnDateSetListener dateSetListener =
    new DatePickerDialog.OnDateSetListener() {
 
	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
 
                //create new Calendar object for date chosen
                //this is done simply combine the three args into one
		Calendar newDate = Calendar.getInstance();
		newDate.set(year, monthOfYear, dayOfMonth);
		//call back to the DateDialogFragment listener
		sListener.dateDialogFragmentDateSet(newDate);
 
	}
};


Step 5: Show a DateDialogFragment

Now that we have defined our DialogFragment subclass that provides a DatePickerDialog, we can use it by invoking it from an Activity.


//create new DateDialogFragment
DateDialogFragment ddf = DateDialogFragment.newInstance(this, R.string.set_date, date);
 
ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
 
    @Override
    public void dateDialogFragmentDateSet(Calendar date) {
        // update the fragment
	mDateDetailFragment.updateDate(date);
    }
});
 
ddf.show(getSupportFragmentManager(), "date picker dialog fragment");


And that does it. You now have a reusable DialogFragment that provides a familiar DatePickerDialog with all its expected behavior.

Note: This solution is backwards compatible for pre-Honeycomb Android. Simply use theAndroid compatibility package.

For complete source code of a simple app that makes use of the DateDialogFragment,click here.


源码:

http://www.kylebeal.com/wp-content/uploads/2011/11/DateDialogFragmentExample.zip

相关文章
|
6月前
|
Java API Android开发
Android使用DatePickerDialog显示时间
本示例展示了如何通过Android的Calendar类获取当前年月日,并使用DatePickerDialog实现日期选择功能。点击TextView弹出日期选择对话框,用户选择后更新显示。注意:Calendar.MONTH值从0开始,需加1修正。布局含一个TextView,Java代码完成日期获取、对话框创建与回调处理。
113 3
|
XML Android开发 数据格式
Android面试题之DialogFragment中隐藏导航栏
在Android中展示全屏`DialogFragment`并隐藏状态栏和导航栏,可通过设置系统UI标志实现。 记得在布局文件中添加内容,并使用`show()`方法显示`DialogFragment`。
261 2
|
Android开发
Android DialogFragment 的封装
Android DialogFragment 的封装
Android DialogFragment 的封装
|
Android开发
关于安卓DialogFragment使用(三)
关于安卓DialogFragment使用(三)
497 0
|
Android开发
关于安卓底部dialogfragment封装
关于安卓底部dialogfragment封装
557 0
|
Android开发
Android 日期选择器之DatePickerDialog
Android 日期选择器之DatePickerDialog
436 0
Android 日期选择器之DatePickerDialog
|
Android开发
关于安卓DialogFragment基类封装
安卓DialogFragment基类封装
284 0
|
Android开发
Android Studio 日期弹窗DatePickerDialog实例
本文目录 1. 功能 2. 布局 3. 代码 4. 效果
696 0
Android Studio 日期弹窗DatePickerDialog实例
|
Android开发 容器
Android开发 - 解决DialogFragment在全屏时View被状态栏遮住的问题
我的上一篇文章:设置DialogFragment全屏显示 可以设置对话框的内容全屏显示,但是存在在某些机型上顶部的View被状态栏遮住的问题。经过测试,发现了一种解决办法,在DialogFragment的onCreateView()中添加一个布局监听器: @Override public View.
2647 0

热门文章

最新文章