TimePickerDialog和DatePickerDialog使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/84030528

TimePickerDialog

TimePickerDialog(Context context, 
				TimePickerDialog.OnTimeSetListener listener, 
				int hourOfDay, 
				int minute, 
				boolean is24HourView)

参数

context:当前上下文
listener:时间改变监听器
hourOfDay:初始化的小时
minute:初始化的分钟
is24HourView:是否以24小时显示时间

在这里插入图片描述
关键代码:

new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                textView.setText(hourOfDay+":"+minute);
            }
        }, 0, 0, true).show();

DatePickerDialog

用法和TimePickerDialog相同
首先调用Calendar类获取年月日,然后将获取到的年月日放进new出来的DatePickerDialog中,这样就可以默认选中当前日期

private void chooseDate(){
        Calendar ca = Calendar.getInstance();

        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        final String data =  year+"年"+(month+1) + "月" + dayOfMonth + "日 ";
                        textView.setText(data);
                    }
                },
                mYear, mMonth, mDay);
        datePickerDialog.show();
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/84030528
今日推荐