Date Picker with cancel option in android

烂漫一生 提交于 2019-12-06 16:46:34

问题


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);

回答1:


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)




回答2:


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.




回答3:


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*/ }
}
}


来源:https://stackoverflow.com/questions/13932272/date-picker-with-cancel-option-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!