我知道它的後期解決方案(我不想在這任何一個浪費時間),這可能是這樣。(感謝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*/ }
}
}
沒試過,但應該對可能這需要擴展取消按鈕,並在此實現您自己的取消按鈕功能 –
Android上的標準是按系統後退按鈕以取消對話框 - 監聽器不會被觸發。 – ataulm
@ataulm即使我這樣做,選定的日期不會被清除。 – lokoko