Date Picker with cancel option in android

2019-04-17 13:14发布

Is it possible to have a date/time picker that has a Cancel/Set options in place of a single Set command. My usecase is that when i click on the date picker and the field is not mandatory, i might want to clear the selected value. My code looks something like this :

private DatePickerDialog datePicker = null;

datePicker = new DatePickerDialog(getContext(), new DateSetListener(), year, monthOfYear, dayOfMonth);

3条回答
成全新的幸福
2楼-- · 2019-04-17 13:50

I would use a simple Dialog and within it include a DateTimePicker and the SET and CANCEL buttons or any other king of functionality you would like to include

Another UI/X solution you can implement is to show the DateTimePicker on demand (user clicking a button for example)

查看更多
戒情不戒烟
3楼-- · 2019-04-17 13:51

There is an issue with the DatePickerDialog and the TimePickerDialog (Issue #34833). We could use a counter as a work-around to get rid of this issue.

查看更多
男人必须洒脱
4楼-- · 2019-04-17 14:03

I know Its Late a solution (i don't want any one waste time on this)for this may look like this.. (Thanks to https://stackoverflow.com/users/642161/dmon)

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;

import java.lang.reflect.Field;

/**
 * Created by root on 12/8/15.
 */
public class DatePickerDialogFragment extends DatePickerDialog {

public DatePickerDialogFragment(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
    super(context, null, year, monthOfYear, dayOfMonth);
    initializePicker(callBack);
}

public DatePickerDialogFragment(Context context,int theme,OnDateSetListener callBack,int year, int monthOfYear, int dayOfMonth)
{
    super(context, theme, null, year, monthOfYear, dayOfMonth);
    initializePicker(callBack);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCancelable(false);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    dismiss();

}

private void initializePicker(final OnDateSetListener callback) {
    try {
        //If you're only using Honeycomb+ then you can just call getDatePicker() instead of using reflection
        Field pickerField = DatePickerDialog.class.getDeclaredField("mDatePicker");
        pickerField.setAccessible(true);
        final DatePicker picker = (DatePicker) pickerField.get(this);
        this.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getText(android.R.string.cancel), (OnClickListener) null);
        this.setButton(DialogInterface.BUTTON_POSITIVE, getContext().getText(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        picker.clearFocus(); //Focus must be cleared so the value change listener is called
                        callback.onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
                    }
                });
    } catch (Exception e) { /* Reflection probably failed*/ }
}
}
查看更多
登录 后发表回答