Android开发小知识-TimePickerDialog和DatePickerDialog的使用

须知

compileSdkVersion 23
buildToolsVersion 23.0.2
minSdkVersion 15
targetSdkVersion 23

本例使用DialogFragment来管理对应控件,这是官方文档中推荐的方式,这样可将控件的生命周期交给DialogFragment管理。
以下是API说明:
We recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.

小例子

1、效果图
Android开发小知识-TimePickerDialog和DatePickerDialog的使用_第1张图片Android开发小知识-TimePickerDialog和DatePickerDialog的使用_第2张图片
2、定义一个类,继承DialogFragment并实现DatePickerDialog.OnDateSetListener。这里使用DialogFragment来管理DatePickerFragment ,由DatePickerDialog.OnDateSetListener的对象负责监听时间的设置。

/**
 * Created by mindto on 2016/3/10.
 * 定义一个类,继承DialogFragment并实现DatePickerDialog.OnDateSetListener
 */
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    //覆写onCreateDialog方法,返回DatePickerDialog对象
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //实例化一个日历对象,通过该对象获取当前的年,月,日,用于初始化DatePickerDialog上的日期
        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);
        //返回一个DatePickerDialog实例
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    //当设置完日期后,后调用该方法
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Toast.makeText(getActivity(), "当前日期设置为:" + year + "年" + monthOfYear + "月" + dayOfMonth + "日", Toast.LENGTH_LONG).show();
    }
}

2、定义一个类,继承DialogFragment并实现TimePickerDialog.OnTimeSetListener实现监听

/**
 * Created by mindto on 2016/3/10.
 * 定义一个类,继承DialogFragment并实现TimePickerDialog.OnTimeSetListener实现监听
 */
public class TimePickerFragment extends DialogFragment
        implements OnTimeSetListener
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //得到日期对象
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), this, hour, minute, true);
    }
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(getActivity(), "当前时间设置为: " + hourOfDay + " : " + minute, Toast.LENGTH_LONG).show();
    }
}

3、活动代码比较简单,负责生成上面类的实例,并显示出来。

/**
 * 布局文件中有两个按钮,有onClick属性
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View view){
        switch (view.getId()){
            //根据ID判断点击哪个按钮,显示不同的设置界面
            case R.id.timePicker:
            //生成DialogFragment实例,调用show()方法显示
                DialogFragment dialogFragment = new TimePickerFragment();
                dialogFragment.show(getFragmentManager(), "timePicker");
                break;
            case R.id.datePicker:
                DialogFragment dialogFragment1 = new DatePickerFragment();
                dialogFragment1.show(getFragmentManager(), "datePicker");
                break;
        }
    }
}

该篇结束,若有不足,望不吝赐教!

你可能感兴趣的:(Android开发系列)