Android DatePickerDialog Api 23 (Android 6)

Jos*_*ala 5 android android-5.1.1-lollipop android-6.0-marshmallow

我正在触发一个 DatePickerDialog,它工作并显示良好,直到 api 22 (Android 5.1),我在其上设置混合和最大日期(最小 = 当前日期,最大 = 从当前日期开始的 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);

    }
} 
Run Code Online (Sandbox Code Playgroud)

安卓5.1

安卓6

答案 1:结果答案 1:结果

Jos*_*ala 1

我在我的主题中使用了这种风格:

<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> 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

结果正如预期的那样。