我正在触发一个DatePickerDialog,它工作正常,直到api 22(
Android 5.1),我正在设置混合和最大日期(min =当前日期,max =从当前日期开始的1个月),但它只是显示当前日期在Api 23中,我附上了代码和图像.
///////////////////////////////datepickerdialog///////////////////////////////
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
/*
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
*/
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(),this,year,month,day);
if (Build.VERSION.SDK_INT < 22){
dpd.getDatePicker().setCalendarViewShown(true);
dpd.getDatePicker().setSpinnersShown(false);
dpd.getDatePicker().getCalendarView().setShowWeekNumber(false);
}
calendar.setTimeInMillis(calendar.getTimeInMillis());
long mindate = calendar.getTime().getTime();
calendar.add(Calendar.MONTH, 1);
long maxdate = calendar.getTime().getTime();
dpd.getDatePicker().setMinDate(mindate);
dpd.getDatePicker().setMaxDate(maxdate);
dpd.getDatePicker().setMinDate(mindate);
if(Build.VERSION.SDK_INT < 23){
dpd.setTitle("");
}
// Return the DatePickerDialog
return dpd;
}
@SuppressLint("SimpleDateFormat")
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
month++;
evento_fecha = year+"-"+month+"-"+day;
month--;
datetime.set(Calendar.YEAR, year);
datetime.set(Calendar.MONTH, month);
datetime.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat mSDF = new SimpleDateFormat("dd/MM/yyyy");
fecha = mSDF.format(datetime.getTime());
//fecha_seleccionada.setText(dia+"/"+mes+"/"+year);
fecha_seleccionada.setText(fecha);
}
}
最佳答案 我在我的主题中使用这种风格:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textAllCaps">false</item>
</style>
textColorPrimary属性显示白色的所有日期数字,我使用此属性以将操作栏文本设置为白色.我错了,因为我必须为此目的使用Theme.AppCompat.Light.DarkActionBar,所以我将主题样式更改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textAllCaps">false</item>
</style>
结果如预期.