美文网首页
Android DatePicker和TimePicker

Android DatePicker和TimePicker

作者: CrazyBoomer | 来源:发表于2017-02-28 22:24 被阅读0次

    1.在 xml中布置Datepicker和TimePicker

        <DatePicker android:id="@+id/dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TimePicker android:id="@+id/tp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    

    2.在Activity中初始化

        private TimePicker tp;
        private DatePicker dp;
        protected void onCreate(Bundle savedInstanceState) {
            ....
            dp=(DatePicker)findViewById(R.id.dp);
            tp=(TimePicker)findViewById(R.id.tp);
         }
    

    3.初始化calendar类

        private Calendar cl;
        private int year,month,day,hour,minute;
        protected void onCreate(Bundle savedInstanceState) {
              ·····
            cl=Calendar.getInstance();
            year=cl.get(Calendar.YEAR);
            month=cl.get(Calendar.MONTH)+1;
            day=cl.get(Calendar.DAY_OF_MONTH);
            hour=cl.get(Calendar.HOUR_OF_DAY);
            minute=cl.get(Calendar.MINUTE);
            setTitle(year+"-"+month+"-"+day+"-"+hour+"-"+minute);
        }
    

    WARNING

    month=cl.get(Calendar.MONTH)+1;

    其中get(Calendar.MONTH)方法得到的数从0开始

    setTitle(year+"-"+month+"-"+day+"-"+hour+"-"+minute);

    该代码用于改变标题

    4.初始化DatePicker的日期

            dp.init(year, cl.get(Calendar.MONTH), day, new OnDateChangedListener() {
                
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                }
            });
    

    WARNING

    dp.init(year, cl.get(Calendar.MONTH), day, new OnDateChangedListener()

    该方法中,月份需要用从0开始的Calendar.MONTH

    setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);

    传入参数为从0开始的月数,在传出时需要+1

    5.初始化TimePicker的时间,并实现时间选择器

            tp.setOnTimeChangedListener(new OnTimeChangedListener() {
                
                @Override
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                    // TODO Auto-generated method stub
                    setTitle(hourOfDay+":"+minute);
                }
            });
    

    6.实现日期选择对话框和时间选择对话框

            new DatePickerDialog(this,new OnDateSetListener() {
                
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                }
            }, year,cl.get(Calendar.MONTH), day).show();
    
    
            new TimePickerDialog(this, new OnTimeSetListener() {
                
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    // TODO Auto-generated method stub
                    setTitle(hourOfDay+":"+minute);
                }
            }, hour, minute, true).show();
    

    同理,注意传入的是Calendar.MONTH是以0开头的月数
    new TimePickerDialog()方法的最后一个参数是是否选择24小时制(true or false)

    相关文章

      网友评论

          本文标题:Android DatePicker和TimePicker

          本文链接:https://www.haomeiwen.com/subject/gfknwttx.html