Android程序:TimePicker和DatePicker的两种实现案例

实现的效果:默认在标题栏中显示当前时间,通过TimePicker和DatePicker改变日期和实现并显示在标题上

public class MainActivity extends Activity implements OnClickListener {

    private TimePicker timePicker;
    private DatePicker datePicker;
    private Calendar cal;
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    private Button bt1;
    private Button bt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取日历的对象
        cal = Calendar.getInstance();

        // 获取对应的年月日时分秒
        year = cal.get(Calendar.YEAR);
        // 当前月份是获取的月份+1
        month = cal.get(Calendar.MONTH) + 1;
        day = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);
        // 将年月日时分秒显示在标题栏中
        setTitle(year + "年" + month + "月" + day + "日  " + hour + ":" + minute);

        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        // 初始化datePicker,使用OnDateChangedListener监听日历选择器的改变并设置标题栏
        datePicker.init(year, month, day, new OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int yearC,
                    int monthOfYear, int dayOfMonth) {
                year = yearC;
                month = monthOfYear + 1;
                day = dayOfMonth;
                setTitle(year + "年" + month + "月" + day + "日  " + hour + ":"
                        + minute);
            }
        });

        // 不需要初始化,直接设置监听器实现方法就可以
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay,
                    int minuteC) {
                hour = hourOfDay;
                minute = minuteC;
                setTitle(year + "年" + month + "月" + day + "日  " + hour + ":"
                        + minute);
            }
        });

        bt1 = (Button) findViewById(R.id.button1);
        bt2 = (Button) findViewById(R.id.button2);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            // 对话框式的datePicker
            new DatePickerDialog(this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int yearC,
                        int monthOfYear, int dayOfMonth) {
                    year = yearC;
                    month = monthOfYear + 1;
                    day = dayOfMonth;
                    setTitle(year + "年" + month + "月" + day + "日  " + hour
                            + ":" + minute);

                }
            }, year, cal.get(Calendar.MONTH), day).show();
            break;
        case R.id.button2:
            // 对话框式的timepicker

            new TimePickerDialog(this, new OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay,
                        int minuteC) {
                    hour = hourOfDay;
                    minute = minuteC;
                    setTitle(year + "年" + month + "月" + day + "日  " + hour
                            + ":" + minute);
                }
            }, hour, minute, true).show();
            break;

        default:
            break;
        }
    }
}

你可能感兴趣的:(安卓备忘录,android,datepicker,timepicker)