私はそれが遅い解決策であることを知っています(私はこれに時間を無駄にしたくありません) 。
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*/ }
}
}