0

是否可以使用具有取消/设置选项的日期/时间选择器来代替单个设置命令。我的用例是,当我单击日期选择器并且该字段不是强制性的时,我可能想清除选定的值。我的代码看起来像这样:

private DatePickerDialog datePicker = null;

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

3 回答 3

2

我会使用一个简单的对话框,其中包含一个 DateTimePicker 以及 SET 和 CANCEL 按钮或您想要包含的任何其他功能之王

您可以实现的另一个 UI/X 解决方案是按需显示 DateTimePicker(例如,用户单击按钮)

于 2012-12-18T11:50:49.193 回答
0

DatePickerDialog 和 TimePickerDialog 存在问题(问题 #34833)。我们可以使用计数器来解决这个问题。

于 2013-01-31T09:15:39.030 回答
0

我知道它晚了一个解决方案(我不希望任何人在这上面浪费时间)因为这可能看起来像这样..(感谢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*/ }
}
}
于 2015-08-12T06:17:44.817 回答