开发者

Date AND time settings dialog in Android

开发者 https://www.devze.com 2023-01-27 03:26 出处:网络
How can I create date and time settings dialog in Android? I want: 3 fields - day, hours and minutes it should look native to the system - that mak开发者_StackOverflowes writing my own widget quite

How can I create date and time settings dialog in Android? I want:

  • 3 fields - day, hours and minutes
  • it should look native to the system - that mak开发者_StackOverflowes writing my own widget quite hard - how to make the plus/minus buttons above/below each field look like the standard buttons that are in TimePickerDialog? I mean even on HTC Sense.


Create buttons that you'll use to both display the date and time, and trigger the date and time dialogs.

Example code :

    fromDatePicker = (Button) findViewById(R.id.fromDatePicker);
    fromDatePicker.setText(dateNow);
    fromDatePicker.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(FROM_DATE_DIALOG_ID);
        }
    });

    fromTimePicker = (Button) findViewById(R.id.fromTimePicker);
    fromTimePicker.setText(timeNow);
    fromTimePicker.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(FROM_TIME_DIALOG_ID);
        }
    });

Create your dialogs like this :

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case FROM_TIME_DIALOG_ID:
            return new TimePickerDialog(this,fromTimeSetListener, fromHour, fromMinute, false);
        case FROM_DATE_DIALOG_ID:
            return new DatePickerDialog(this,fromDateSetListener,fromYear, fromMonth, fromDay);
        case TO_TIME_DIALOG_ID:
            return new TimePickerDialog(this,toTimeSetListener, toHour, toMinute, false);
        case TO_DATE_DIALOG_ID:
            return new DatePickerDialog(this,toDateSetListener,toYear, toMonth, toDay);                
    }
    return null;
}

Your screen will look like this :

Date AND time settings dialog in Android

When clicking the date button, a datepicker will popup looking like this :

Date AND time settings dialog in Android

When clicking the time button, a timepicker will popup looking like this :

Date AND time settings dialog in Android

Android doesn't offer a combined DateTimePicker dialog out of the box... If you want to combine both the date and timepicker on 1 screen or dialog you have 3 options :

Work with the UI widgets in a layout/view :

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />  

<DatePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />  

Find a third party component offering this functionality

There's a custom datetimepicker at http://code.google.com/p/datetimepicker

Write one yourself

0

精彩评论

暂无评论...
验证码 换一张
取 消